Hi all.
I’ve got a problem with my camera system.
The “cam” function at the bottom should move the background group by the inverse of the players velocity, (which is monitored each frame by the “velocity” function).
Everything works perfectly, but the background will not reset back to its original position. With each bounce the background gets lower and lower.
I’m aware of the problem with box2D, whereby an object will bounce slightly higher each time, but that shouldn’t effect the background moving back to the same position each time.
Spider.
[code]
physics = require(“physics”)
require “sprite”
physics.start()
physics.setGravity(0,9.8)
_H = display.contentHeight;
_W = display.contentWidth;
local masterGroup = display.newGroup()
local backgroundGroup = display.newGroup()
local playerGroup = display.newGroup()
masterGroup:insert(backgroundGroup)
masterGroup:insert(playerGroup)
player1 = display.newRect (0, 0, 30, 30)
player1.x = _W / 2
player1.y = 100
player1:setFillColor(0, 0, 255)
physics.addBody(player1, {density = 1.0, friction = 0, bounce = 1})
playerGroup:insert(player1)
function velocity() – gets the players velocity in x and y
vx, vy = player1:getLinearVelocity()
end
Runtime:addEventListener(“enterFrame”, velocity) --checks the players velocity each frame
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})
playerGroup:insert(ground)
local textObject = display.newText( “Background”, 0, 0, “courier”, 60 )
textObject.x = _W / 2
textObject.y = _H / 2
textObject:setTextColor( 255, 0, 0 )
textObject.rotation = 46
backgroundGroup:insert(textObject)
function cam()
if player1.y < 96 then
repeat masterGroup.y = masterGroup.y - vy/20 until masterGroup.y <= _H
end
end
Runtime:addEventListener(“enterFrame”, cam)
[/code] [import]uid: 67933 topic_id: 21596 reply_id: 321596[/import]