Discuss Scratch

Mr_Jacobs123
New to Scratch
4 posts

Stopping a sprite moving off a screen

Hi,

I have a sprite moving around a screen with the standard code below. There is an ‘enemy’ which is trying to chase it. What is the best way to stop it from moving outside of the screen borders?

when green flag clicked
forever
if <key [ up arrow] pressed?> then
move (10) steps
end
end

I was thinking of ‘if x position > than 240 then move -10 steps’ and then doing that for each quadrant of the stage, but is there a more elegant solution?
kittenslikefood
Scratcher
100+ posts

Stopping a sprite moving off a screen

Mr_Jacobs123 wrote:

Hi,

I have a sprite moving around a screen with the standard code below. There is an ‘enemy’ which is trying to chase it. What is the best way to stop it from moving outside of the screen borders?

when green flag clicked
forever
if <key [ up arrow] pressed?> then
move (10) steps
end
end

I was thinking of ‘if x position > than 240 then move -10 steps’ and then doing that for each quadrant of the stage,
but is there a more elegant solution?[/quote]

Hi!
I am no expert but maybe you could use the
if on edge, bounce
block?

Hope that helps

Last edited by kittenslikefood (Oct. 8, 2018 07:40:04)

Mr_Jacobs123
New to Scratch
4 posts

Stopping a sprite moving off a screen

kittenslikefood wrote:

Mr_Jacobs123 wrote:

Hi,

I have a sprite moving around a screen with the standard code below. There is an ‘enemy’ which is trying to chase it. What is the best way to stop it from moving outside of the screen borders?

when green flag clicked
forever
if <key [ up arrow] pressed?> then
move (10) steps
end
end

I was thinking of ‘if x position > than 240 then move -10 steps’ and then doing that for each quadrant of the stage,
but is there a more elegant solution?[/quote]

Hi!
I am no expert but maybe you could use the
if on edge, bounce
block?

Hope that helps[/quote]

Hey, thanks for the idea. I did try this, but this makes the sprite 'flip' off the edge and keep moving. I'd prefer a solution where it tries to go into the wall and is pushed back.
Scratch-Minion
Scratcher
1000+ posts

Stopping a sprite moving off a screen

Your test for moving off the screen looks fine except that you could test that you won't move off the screen before moving so that you don't have to then move back.

if <<key [up arrow v] pressed?> and <(x position) < [240]>> then
move (10) steps
end

This assumes that you start at (0,0) and always move 10 steps.
Otherwise you would need to check that (x position + 10) < 241 to keep the sprite's centre on the screen!


Also note that in the code above, x position is the centre of the sprite, so half your sprite may be off the stage!
This can be fixed by using a number less than 240 as the limit to keep the whole sprite on the stage. The number will depend on the distance from the centre of your sprite to the right side of the sprite at the right of the stage. Different numbers may be needed at the left, top and bottom of the stage.

Note that you should have your sprite costume centered.
ason13345
Scratcher
1 post

Stopping a sprite moving off a screen

I want to stop my car from going off the screen as so as my other one, and I wan to stop it without that up key thing.
Apartmarrow5421
New to Scratch
1 post

Stopping a sprite moving off a screen

I have this same problem, but these posts didn't exactly give me a solution.
ayyyyes10135
Scratcher
100+ posts

Stopping a sprite moving off a screen

dont put a forever block, doing that makes it go forever use a
repeat until <not right arrow key pressed>
move (10) steps
end

Last edited by ayyyyes10135 (Jan. 29, 2021 21:16:50)


YOOOOOOO 100 POSTS LETS GO

________________________________________

give me ideas on what to create. im so bored every day. yeah i should probably doing my project but NO IDEAS!!!
blliibi
Scratcher
1 post

Stopping a sprite moving off a screen

Apartmarrow5421 wrote:

I have this same problem, but these posts didn't exactly give me a solution.
hello you can do it by making a varibel X and a Y
set [ X] to (x position)
set [ Y] to (y position)
forever
if <touching [ edge] ?> then
go to x: (X) y: (Y)
end
set [ X] to (x position)
set [ Y] to (y position)
end
RayirthSMD2W
Scratcher
1 post

Stopping a sprite moving off a screen

how do I make a sprite not go off screen !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
MC_Hero09
Scratcher
89 posts

Stopping a sprite moving off a screen

Just use less than or greater than blocks
when green flag clicked
forever
if <key [ up arrow] pressed?> then
change y by (4)
end
if <(y position) > [240]> then
change y by (-4)
end
end
Just an example

Powered by DjangoBB