Repeating 2 backgrounds in physics game

Hi,

I’m making pretty simple demo using these free graphics: http://www.vickiwenderlich.com/2013/02/free-game-art-flying-goldfish/

I’ve created a fish object, which has physics body. On tap:

local function boostFish(e)     fish:applyLinearImpulse( 90, -90, fish.x, fish.y) end

So it boosts Fish.

The problem is I’m moving a camera (http://developer.coronalabs.com/code/move-camera): camera.x = -fish.x + screenWidth / 2 in enterFrame event and it centers screen on fish, however I want to loop 2 backgrounds (see images in link) to make effect of moving world and I don’t know how to calculate their positions to make such effect. 

Any help will be appreciated,

Regards

I figured out something like that:

&nbsp; local function updateFrame(event) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local dfx = lfx - fish.x lfx = fish.x camera.x = -fish.x + screenWidth / 2 --print("dfx = " .. dfx) bg1.x = bg1.x + dfx bg2.x = bg2.x + dfx s = s + dfx if mAbs(s) \> screenWidth then &nbsp;&nbsp;&nbsp;&nbsp;m = sgn(s) &nbsp;&nbsp;&nbsp;&nbsp;if m ~= 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = s - m \* screenWidth &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if m \< 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bg1.x = bg1.x - 2 \* m \* screenWidth &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bg2.x = bg2.x - 2 \* m \* screenWidth &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;Runtime:addEventListener( "enterFrame", updateFrame ); &nbsp;&nbsp;&nbsp;&nbsp;s = screenLeft - screenWidth

However, I can only first background and it’s sometimes jumping. Anyone help :smiley: ?

I figured out something like that:

&nbsp; local function updateFrame(event) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local m &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local dfx = lfx - fish.x lfx = fish.x camera.x = -fish.x + screenWidth / 2 --print("dfx = " .. dfx) bg1.x = bg1.x + dfx bg2.x = bg2.x + dfx s = s + dfx if mAbs(s) \> screenWidth then &nbsp;&nbsp;&nbsp;&nbsp;m = sgn(s) &nbsp;&nbsp;&nbsp;&nbsp;if m ~= 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;s = s - m \* screenWidth &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if m \< 0 then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bg1.x = bg1.x - 2 \* m \* screenWidth &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bg2.x = bg2.x - 2 \* m \* screenWidth &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end end &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;Runtime:addEventListener( "enterFrame", updateFrame ); &nbsp;&nbsp;&nbsp;&nbsp;s = screenLeft - screenWidth

However, I can only first background and it’s sometimes jumping. Anyone help :smiley: ?