Page 1 of 1

Make an object appear on the other side of the screen

Posted: Thu Sep 26, 2013 3:26 pm
by nice
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.

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

Posted: Thu Sep 26, 2013 3:54 pm
by micha
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.

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

Posted: Thu Sep 26, 2013 3:56 pm
by nice
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?

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

Posted: Thu Sep 26, 2013 4:10 pm
by IMP1
Alternatively, you could use the modulo (https://en.wikipedia.org/wiki/Modulo_operation) operator.

Code: Select all

x = x % love.graphics.getWidth()

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

Posted: Thu Sep 26, 2013 5:22 pm
by nice
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!