Window limits

Hi!

I am new to the forum, and programming. This I do for entertainment.

I tried to put limits on the player window, and the heart, which is the enemy off the screen, as shown in the video I posted below.
I used Video Game controller that posting Nat

I would appreciate someone guide me to add the limits.

Ps: sorry for my bad English

http://www.youtube.com/watch?v=3dbHY-_TNGA&feature=youtu.be

Hello @Anak, do you mean limits to how far a sprite can go ?
Something like should work fine:

if sprite.x > WIDTH then
   sprite.x = WIDTH
end
if sprite.x < 0 then
   sprite.x = 0
end

if sprite.y > HEIGHT then
   sprite.y = HEIGHT
end

if sprite.y > 0 then
   sprite.y = 0
end

you could also do:

math.max(0, math.min(WIDTH, sprite.x))
math.max(0, math.min(HEIGHT, sprite.y))

which would make code a bit more tidy, although a tiny bit slower (you won’t notice it)

Cheers

@Xavier: actually, your version using math.max/math.min won’t do anything unless you assign the result :wink:


sprite.x = math.max(0, math.min(WIDTH, sprite.x))
sprite.y = math.max(0, math.min(HEIGHT, sprite.y))

@toadkick - haha yeah forgot that ty :wink: