I can't figure out the camera

I just can’t grok the camera.

I am trying to make a 2D game in corona labs.

Basically you have a sphere(blue circle currently) and you can move it up or down on the screen (if you move it into one of the sides there is a wall and it bounces off. )

In order to move it up and down I want to make a camera that follows the player object.

I have two issues:

  1. The background should be infinite and in order to get that I want to take my background images and shift them as they go off screen. So when a background image move off screen because the player is going downwards the image should shift to below the screen of the player. And vice versa if he is going up.
  2. The camera should have have a “rubberband” effect, that is instead of constantly following the player, it should only start following the player after a little bit, then go slightly faster than the player until it catches up and then be centered around the player again.

I am not entirely sure how to do either of these things.

For the first issue, I have tried something like:

 

[lua]

//If background 1 is below the screen (the player object is moving away from the bottom), set it to be above the screen instead (the player object is moving towards the top)
    if background1 < 0 - display.contentHeight then
        background1.y = display.contentHeight
    end
//If background 1 is above the screen (player object is falling downwards), instead set it to be below the screen (player object is falling towards it)
    if background1 > (display.contentHeight * 2) then
        background1.y = 0 - display.contentHeight + 10
    end
[/lua]

And connect the above code to the same method that updates the camera.

For the second issue I have the following code:

[lua]

local camera ={}

ly = 0

function camera.init(self, trackObj)
    ly = trackObj.y
end

function camera.update(trackObj, world)

    local dy = trackObj.y - ly    
    ly = trackObj.y
        
    if trackObj.y > 400 or trackObj.y < 100 then
        world.y = world.y - dy
    end

end
[/lua]

And in the main.lua :

[lua]local camera = require(“scripts.camera”);
camera:init(player)

local function bgScroll(event)
    camera.update(player, layers.world)
end

Runtime:addEventListener(‘enterFrame’, bgScroll)
[/lua]

Which works okay. The problem is the rubberband effect will only work from the starting position. I am unsure of how to make it follow the player object properly.

In order to get things to display in the proper order (background on bottom, then terrain and the player object) I also have everything organized into layers.

[lua]
layers = display.newGroup()
layers.world = display.newGroup()
layers.content1 = display.newGroup()
layers.terrain = display.newGroup()

layers:insert(layers.world)
layers.world:insert(layers.content1)

layers.terrain:insert(flipper)
layers.terrain:insert(angled)
layers.terrain:insert(angled2)
layers.content1:insert(background1)
layers.content1:insert(background2)

layers.content1:insert(layers.terrain)
layers.content1:insert(player)
[/lua]

I think the layers might be what is confusing me, I am not sure how to properly manipulate the positions of objects in them.

I have been trying to read some of the documentation and also look in the forums but I can’t quite figure it out. I was wondering if anyone here could help me?