Ok, so I have a very simple camera which moves in the opposite direction to the player, when the player object bounces up towards the top of the screen. When the player starts to fall, I’d like the ground to move back to it’s original position, matching the players speed, (it’s for a half pipe skating game).
I’ve got the camera working fine, but it refuses to move the “masterGroup” back to it’s original position. There appears to be an offset of roughly 10.2 pixels every time. This may be down to BOX2D making the player1 object bounce higher each time. I’m not worried how high he goes, just so long as the masterGroup moves back to it’s original position each time.
I’ve been mulling this problem for ages, and am now really stuck, I’ve tried everything I can think of. Is it maybe a bug? Can someone please offer me some advice?
physics = require("physics")
physics.start()
physics.setGravity(0,9.8)
\_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
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
if player1.y \< 96 then
repeat masterGroup.y = masterGroup.y - vy/20 until masterGroup.y \<= \_H
print (masterGroup.y)
end
end
end
Runtime:addEventListener("enterFrame", cam)
end
function startGame()
physObjects()
background()
dynamicCam()
end
startGame()
Thanks [import]uid: 67933 topic_id: 16678 reply_id: 316678[/import]
[import]uid: 52491 topic_id: 16678 reply_id: 62482[/import]