Discuss Scratch

Madysen
New Scratcher
8 posts

Issue coding for 'obstacles'

http://scratch.mit.edu/projects/17305446/ If someone could take a look at the code for this and explain my problem, I'd be very grateful.

I don't want the character to be able to go beyond the edges of the room. So I tried to make it so that when the sprite is touching the grey colour, he moves in the opposite direction. Therefore ‘bouncing’ off the walls.

Only, if you fiddle around with the arrow keys, you can make him touch a wall and not bounce. Then, when you try to move away from the wall, you instead end up moving INTO the wall.

And I don't want my walls eating people. :\
ErnieParke
Scratcher
1000+ posts

Issue coding for 'obstacles'

The problem with your code is that you check if you're touching grey after you've moved into it. So for example, let's say you walk left into the wall. When you try to walk to the right, your sprite goes, I'm touching grey! Error! Get me out of this wall! And then it moves in the wrong direction, or in other words, left. That's because it thinks the wall is on the right.

One way you could fix this is that instead of using:

when [left arrow v] key pressed
switch to costume [LeftC v]
if (touching color [#555]?) then
change x by (15)
else
change x by (-10)
end

Try using:

when [left arrow v] key pressed
switch to costume [LeftC v]
change x by (-15)
if (touching color [#555]?) then
change x by (15)
end

By the way, welcome Madysen to Scratch! I hope that you'll have a fun time here!

Welcome,

ErnieParke
Madysen
New Scratcher
8 posts

Issue coding for 'obstacles'

Excellent, thank you so much! I'm still working at figuring out how to recognize the little things like that, but I see what you mean now.

Powered by DjangoBB