I know there have been other topics about camera control but they always focus on horizontal camera movement only. I really need help with vertical as well. The camera group doesn’t follow my player properly at all. If I remove the moveCamera function then the moveP works fine. Here is my code
[lua]display.setStatusBar( display.HiddenStatusBar )
local physics = require(“physics”)
local gameUI = require(“gameUI”)
physics.start()
physics.setGravity(0,0)
local eventX
local eventY
–Create Display Group
local camera = display.newGroup()
camera.x = 0
camera.y = 0
–Draw Images and Load them into Display Group
local bg1 = display.newImageRect( “BG1_01.png” , 480, 320 )
bg1:setReferencePoint( display.CenterLeftReferencePoint )
bg1.x = 0; bg1.y = 160
camera:insert(bg1)
local bg2 = display.newImageRect( “BG2_02.png” , 480, 320 )
bg2:setReferencePoint( display.CenterLeftReferencePoint )
bg2.x = 480; bg2.y = 160
camera:insert(bg2)
local bg3 = display.newImageRect( “BG3_03.png” , 480, 320 )
bg3:setReferencePoint( display.CenterLeftReferencePoint )
bg3.x = 0; bg3.y = 480
camera:insert(bg3)
local bg4 = display.newImageRect( “BG4_04.png” , 480, 320 )
bg4:setReferencePoint( display.CenterLeftReferencePoint )
bg4.x = 480; bg4.y = 480
camera:insert(bg4)
–Draw Player and load into Display Group
local player = display.newImage( “player.png” , 27, 27 )
player.x = 80; player.y = 100
physics.addBody(player, {bounce = 0.5, friction = 0.5, density = 2.0, radius = 10 })
camera:insert(player)
function moveCamera()
if(player.x > 80 and player.x < 1024) then
camera.x = -player.x + 80
end
if(player.y > 100 and player.y < 1024) then
camera.y = -player.y + 100
end
end
Runtime:addEventListener(“enterFrame”, moveCamera)
–Create Function to Drag Player
function stopP()
player:setLinearVelocity(0, 0)
camera:move(0,0)
end
function moveP ()
if t then timer.cancel(t)
end
– PLayer speed
vmax = 150
dx = eventX-player.x
dy = eventY-player.y
ds = (dx*dx + dy*dy)
scalefactor = (vmax^2/ds)^0.5
vely = dy * scalefactor
velx = dx * scalefactor
traveltime = 1000 * (ds^0.5) / vmax
player:setLinearVelocity(velx, vely)
t = timer.performWithDelay(traveltime, stopP)
end
function l ( event )
if(event.phase == “began”) then
eventX = event.x
eventY = event.y
moveP ()
end
end
camera:addEventListener(“touch”, l)
[lua] [import]uid: 26289 topic_id: 12426 reply_id: 312426[/import]
[import]uid: 3826 topic_id: 12426 reply_id: 45342[/import]