Log In  

Pretty basic notion, but I'm falling over on this...
With a sprite that has a starting X,Y and a speed, I'd like the sprite to remain on screen.

However:

-- locks sprite off screen after passing the screen boundary
-- if btn(0)
--and x<128
-- then//move sprite
-- if x < 128
-- then
-- x=x-speed
-- end
-- end

-- locks sprite centre screen
-- while x < 128 do
-- if btn(0) then
-- x=x-speed
-- end
-- end

P#22025 2016-06-01 06:38 ( Edited 2016-06-02 00:09)

x = mid(0,x+speed,127)
P#22026 2016-06-01 07:17 ( Edited 2016-06-01 11:17)

thanks very much. I'll need to play around with my pixel width for the right hand margin, but that does the trick!

P#22029 2016-06-01 08:11 ( Edited 2016-06-01 12:11)

Yep, hope it makes sense.

mid returns the middle of three values.

If x+speed is < 0 then it'll return 0, if x+speed > 127 then it'll return 127.

You could also write it as:

x += speed
if x < 0 then x = 0 end
if x > 127 then x = 127 end
P#22030 2016-06-01 09:10 ( Edited 2016-06-01 13:10)

How about

x=min(128,max(x+speed,0))

?

P#22032 2016-06-01 10:07 ( Edited 2016-06-01 14:08)

Thanks!

Is there an advantage to that particular method?

(I'll try it, obviously!)

P#22044 2016-06-01 15:54 ( Edited 2016-06-01 19:54)

the mid method is the simplest and uses the fewest tokens.

the if method allows you to insert extra logic such as playing sound effects when you hit the walls or bounce off or something.

the min,max method is equivalent to the mid method.

P#22067 2016-06-01 20:09 ( Edited 2016-06-02 00:09)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 22:35:45 | 0.007s | Q:17