How to create a scrolling background relative to an objects X position?

I am making an app similar to Jetpack Joyride, but I am having trouble creating a background that moves with the character. I’ll have to explain what I am doing for this to make sense. I have a character that constantly moves forward by adding to it’s X. I then have 2 backgrounds (same image) that just spawn next to each other. They don’t move but the character does and I have the “camera” following him so I guess technically they are moving, but they’re coordinates are stationary until I move them. When the image on the left is off the screen, it jumps to being in front. I do this by checking if the character is at an X location that is divisible by 1024. It then jumps the background image that is on the left to the right to make a continuous image.

 if math.round(character.x-112)%1024 == 0 --I say -112 because he is defaulted at 112

This works until I want to speed up the character. To do so, I just change a variable that is being added to the fish’s X coordinate, but this is where I noticed the flaw in this plan. Every number I need to add to the fish must be divisible by 1024 or else it doesn’t detect it and doesn’t move the backgrounds…This means I will have to do huge jumps in his speed for it to actually work. Is there a better way to do this effect, or a simple solution to this problem? Any help is appreciated! :slight_smile: [import]uid: 14461 topic_id: 34296 reply_id: 334296[/import]

[SOLUTION] Alright I figured it out. I wasn’t taking into account the mod of the character. Basically I see if it is at a position that is divisible by 1024, if not it has to be within a certain range (the amount I am moving the character per frame). If it is within that range and the remainder (mod) doesn’t equal 0, I move the background on the left to the right however much works (1440 for me) and then minus the remainder. Heres the code:

if math.round(character.x-112)%1024 \< charSpeed and bgToMove == 1 then bgToMove = 2 -- just a variable that allows me to check which one is on the left bg.x = character.x + 1424 - math.round(character.x-112)%1024 elseif math.round(character.x-112)%1024 \< charSpeed and bgToMove == 2 then bgToMove = 1 -- just a variable that allows me to check which one is on the left bg2.x = character.x + 1424 - math.round(character.x-112)%1024 end [import]uid: 14461 topic_id: 34296 reply_id: 136335[/import]

[SOLUTION] Alright I figured it out. I wasn’t taking into account the mod of the character. Basically I see if it is at a position that is divisible by 1024, if not it has to be within a certain range (the amount I am moving the character per frame). If it is within that range and the remainder (mod) doesn’t equal 0, I move the background on the left to the right however much works (1440 for me) and then minus the remainder. Heres the code:

if math.round(character.x-112)%1024 \< charSpeed and bgToMove == 1 then bgToMove = 2 -- just a variable that allows me to check which one is on the left bg.x = character.x + 1424 - math.round(character.x-112)%1024 elseif math.round(character.x-112)%1024 \< charSpeed and bgToMove == 2 then bgToMove = 1 -- just a variable that allows me to check which one is on the left bg2.x = character.x + 1424 - math.round(character.x-112)%1024 end [import]uid: 14461 topic_id: 34296 reply_id: 136335[/import]