## Make an object appear on the other side of the screen

General discussion about LÖVE, Lua, game development, puns, and unicorns.
nice
Party member
Posts: 188
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

### Make an object appear on the other side of the screen

As the subject says, I have an object that I want to appear on the other side of the screen and I have done this before but I need to have my memory refreshed.
And if I remember correctly it's something like this: then object have a lesser value (or greater) than 0 (or 800) it will appear on the other side.

So if you please could nudge me on the right track I would be grateful.
Have a good day!

micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

### Re: Make an object appear on the other side of the screen

If I understand correctly you want that an object, that exits the screen on the right, reenters on the left? The you are right:

Code: Select all

if x > screenWidth then
x = x - screenWidth
end
if x < 0 then
x = x + screenWidth
end
If you use the LÖVE default screen Width, then it is 800.

nice
Party member
Posts: 188
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

### Re: Make an object appear on the other side of the screen

micha wrote:If I understand correctly you want that an object, that exits the screen on the right, reenters on the left? The you are right:

Code: Select all

if x > screenWidth then
x = x - screenWidth
end
if x < 0 then
x = x + screenWidth
end
If you use the LÖVE default screen Width, then it is 800.
Just to be sure: do I create a separate function for it or put it in love.draw?
Have a good day!

IMP1
Prole
Posts: 39
Joined: Mon Oct 03, 2011 8:46 pm

### Re: Make an object appear on the other side of the screen

Alternatively, you could use the modulo (https://en.wikipedia.org/wiki/Modulo_operation) operator.

Code: Select all

x = x % love.graphics.getWidth()

nice
Party member
Posts: 188
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

### Re: Make an object appear on the other side of the screen

micha wrote:If I understand correctly you want that an object, that exits the screen on the right, reenters on the left? The you are right:

Code: Select all

if x > screenWidth then
x = x - screenWidth
end
if x < 0 then
x = x + screenWidth
end
If you use the LÖVE default screen Width, then it is 800.
Solved it!
The only mistake I did was using 'x' on local and give it a value of 800, which screw up some of the coding because I already have something that describes the x position.

Thanks for the help!
Have a good day!

### Who is online

Users browsing this forum: Bing [Bot] and 3 guests