Make an object appear on the other side of the screen

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

Make an object appear on the other side of the screen

Post 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.
:awesome: Have a good day! :ultraglee:
User avatar
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

Post 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.
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

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

Post 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?
:awesome: Have a good day! :ultraglee:
User avatar
IMP1
Prole
Posts: 43
Joined: Mon Oct 03, 2011 8:46 pm

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

Post by IMP1 »

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

Code: Select all

x = x % love.graphics.getWidth()
User avatar
nice
Party member
Posts: 191
Joined: Sun Sep 15, 2013 12:17 am
Location: Sweden

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

Post 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!
:awesome: Have a good day! :ultraglee:
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 173 guests