Ok, so I’ve written some drag and drop code which demonstrates the problem. I have 2 problems, firstly the movement of the “masterGroup” is MUCH faster than the movement of the player, despite them both moving by “vy.” To correct this in the example I’ve set the “masterGroup” to move by “vy/20” to reduce the speed.
The second problem is that over time, the whole “masterGroup” seems to sink off the screen. It’s as if it doesn’t quite return it’s position to 0.
Can someone please take a look and let me know where I’m going wrong, it’s driving me round the bend!
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()
[import]uid: 67933 topic_id: 16175 reply_id: 60333[/import]