Discuss Scratch

Blinzel
New to Scratch
1 post

limit x position of sprite does not work

Hi,
I made a simple game where a planet falls down and needs to be catched.
This works fine, except sometimes the sprite is outside of screen.
Here is what it looks where limit of y-position works fine:
when green flag clicked
go to [ random position]
set y to (180)
forever
change y by (-5)
if <(y position) < [-170]> then
go to [ random position]
set y to (180)
end
end

When I add a limit for x-position it is simply ignored:
when green flag clicked
go to [ random position]
set y to (180)
forever
change y by (-5)
if <(y position) < [-170]> then
go to [ random position]
if <(x position) > [170]> then
set x to (170)
end
set y to (180)
end
end

Any ideas why?
Any ideas how to implement a x-position limit?
ge-b
Scratcher
100+ posts

limit x position of sprite does not work

When the sprite goes to the random position at the start, it has to fall to y<-170 to check if the x position is outside the boundary.
I'd suggest putting the
if <(x position) >[170]> then
set x to (170)
end
outside of the
if <(y position)<[-170]> then
go to [random position v]
end
part, so it check it all the time (and therefore at the start) instead of only after falling to the bottom.

Also, if you want the x boundary to be both left and right, instead of
<(x position)>[170]>
you can use
 <(x position)>[170]> or <(x position)<[-170]>
and
 set x to (pick random (-170) to (170)) 
instead.

I am a competent human [citation needed]
DerpyHead0
Scratcher
1000+ posts

limit x position of sprite does not work

that script is fine, it's probably something else in the project.
can you share it?

when I start as a clone
delete this clone

Powered by DjangoBB