Help with Display.newgroup(). The camera following the player. Vertically

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]

that’s because you are moving the camera.

there are two things, a viewport and the large set of images that you are creating and then the screen.

[text]
±-----------+ | |
| |
[--------------] | |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
[--------------]
| |
| |
±-----------+
[/text]

so when you move using the camera.x and camera.y, what you are essentially doing is just moving the group (camera) off the Visible screen, while what you need to do is move the contents of Camera instead.

so you need to move the BG1…BG4 to provide the illusion of scrolling.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12426 reply_id: 45342[/import]

I think I get what your saying and it seems logical but most examples i’ve seen involve moving the display.newGroup/Camera. For instance EggBreaker-

Am I missing something?

[lua]local function moveCamera()
if (boulder.x > 80 and boulder.x < 1100) then
game.x = -boulder.x + 80
end
end
Runtime:addEventListener( “enterFrame”, moveCamera )
[import]uid: 26289 topic_id: 12426 reply_id: 45366[/import]

what is camera:move(0,0) ?? comment it and your error should disappear.
secondly, since you are placing the Player in the same group as the background, your scrolling will be limited and have a few limitations as you are moving the player based on the touch co-ordinates.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 12426 reply_id: 45371[/import]

Cheers man

The camera:move(0,0) thing is left over from the bit of code that I nicked from Lime. Removing it does nothing really. I’m going to have a tinker with what you have suggested over the weekend and see if I can make it work. So I put the background images in one display group and the player in another right?

Much appreciated [import]uid: 26289 topic_id: 12426 reply_id: 45702[/import]

I’ve split them into two different display groups. One for the player and one for the background. It does produce a better result however I don’t fully understand what the function below actually does :). Would make it a hell of a lot easier if someone could unravel/break it down for me.

[lua]function moveCamera()

if(player.x > 0 and player.x < 960) then
camera.x = -player.x + 0
end

if(player.y > 0 and player.y < 720) then
camera.y = -player.y + 0
end

end
[import]uid: 26289 topic_id: 12426 reply_id: 45972[/import]