Camera not functioning as it should. (drag and drop code example)

Hi everyone.

I’m programming a camera system so that when my character moves past a certain height on the screen, a group containing all necessary graphics objects moves in the opposite direction, but at the same speed as the character.

This will keep the character at the same point on the screen, and ensure that however fast he is moving, the background moves at the same speed.

I have 2 issues with my code:
1, moving the “masterGroup” by “vy” makes it move MUCH faster than the “player” object, even though they both move by the same value, (vy). I don’t understand why it is doing this?

2, In the “cam” function, the second IF statement should force the “masterGroup” back to its original position on screen. This doesn’t work, and every time the player object bounces it sinks of the bottom of the screen by a few pixels. I’ve tried various solutions but can’t get it working.

Please can someone tell me where I’m going wrong?

[code]
physics = require(“physics”)
physics.start()
physics.setGravity(0,9.8)
–physics.setDrawMode( “hybrid” ) – uncomment to see physics interactions.

_H = display.contentHeight;
_W = display.contentWidth;

local masterGroup = display.newGroup()
local plangroup = display.newGroup()
local playergroup = display.newGroup()

masterGroup:insert(plangroup)
masterGroup:insert(playergroup)

function physObjects()
player1 = display.newRect (0, 0, 30, 30)
player1.x = _W / 2 + 150
player1.y = 41 + player1.contentHeight / 2 – was 35
player1:setFillColor(0, 0, 255)
physics.addBody(player1, {density = 1.0, friction = 0, bounce = 1})
player1.myName = “player1”
playergroup:insert(player1)

function velocity()
vx, vy = player1:getLinearVelocity()
–print (vy)
end
Runtime:addEventListener(“enterFrame”, velocity)

player1.collision = onLocalCollision
player1:addEventListener( “collision”, player1 )

ground = display.newRect (0, 0, _W, 5)
ground.x = _W / 2
ground.y = _H - ground.contentHeight / 2
ground:setFillColor(0, 255, 0)
physics.addBody(ground, “static”, {density = 1.0, friction = 0, bounce = 0.2})
ground.myName = “ground”
playergroup:insert(ground)

end

function background()
back1 = display.newRect (0, 0, _W, 96)
back1.x = _W / 2
back1.y = _H - back1.contentHeight / 2
back1:setFillColor(244,244,244)
plangroup:insert(back1)

back2 = display.newRect (0, 0, _W, 96)
back2.x = _W / 2
back2.y = _H - back1.contentHeight / 2 - 96
back2:setFillColor(215,215,215)
plangroup:insert(back2)

back3 = display.newRect (0, 0, _W, 96)
back3.x = _W / 2
back3.y = _H - back1.contentHeight / 2 - 96 * 2
back3:setFillColor(174,174,174)
plangroup:insert(back3)

back4 = display.newRect (0, 0, _W, 96)
back4.x = _W / 2
back4.y = _H - back1.contentHeight / 2 - 96 * 3
back4:setFillColor(120,120,120)
plangroup:insert(back4)

back5 = display.newRect (0, 0, _W, 96)
back5.x = _W / 2
back5.y = _H - back1.contentHeight / 2 - 96 * 4
back5:setFillColor(65,65,65)
plangroup:insert(back5)
end

function onLocalCollision( self, event )
if ( event.phase == “began” ) then

----------------------------------player hits ground---------------------------------------------------------------------------
if (self.myName == “player1” and event.other.myName == “ground”) then
canCamMove = true
end
end
end

function dynamicCam()

canCamMove = false

function cam()
if canCamMove == true then – canCamMove is set to true when board collides with halfpipe.
if player1.y < 100 then
masterGroup.y = masterGroup.y - vy/20 – instead of “1” use velocity of player1
–print(“works”)
end
end

if canCamMove == true then – moves it back into initial position
if player1.y > 250 then
if masterGroup.y < 0 then
masterGroup.y = masterGroup.y + vy/20
–print (masterGroup.y)
end
end
end

end

Runtime:addEventListener(“enterFrame”, cam)
end

function startGame()
physObjects()
background()
dynamicCam()
end

startGame()
[/code] [import]uid: 67933 topic_id: 16215 reply_id: 316215[/import]