Help with scrolling background and physics objects

Ok so I have my player moving around the level/background and the camera following him. It seems to be working ok. However when I add another physics object (enemy) I have to put it in the same display group as my player right? Otherwise Box2D has a problem with detecting the collision. So I do this but now the object moves along with the camera and my player. This makes for a pretty silly game :slight_smile:

Have I gone about this the right way? I have two display groups. One for my background and another for my player. Or should I use one display group like the Eggbreaker example?

Game Info: The game is set under water and the player needs to be able to move in all directions following the user’s finger. The player will attack enemies on the surface and underwater.

I would be eternally grateful if someone could help.

Thanks

Here is my code

[lua]display.setStatusBar( display.HiddenStatusBar )

local physics = require(“physics”)
local gameUI = require(“gameUI”)

physics.start()
physics.setGravity(0,0)
physics.setDrawMode( “hybrid” )

local eventX = 0
local eventY = 0

–Create Display Groups
camera = display.newGroup()
camera.x = 0
camera.y = 0

movePlayer = display.newGroup()
movePlayer.x = 0
movePlayer.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

player = display.newImage( “player.png” , 27, 27 )
player.xOrigin = 30; player.yOrigin = 50
physics.addBody(player, {bounce = 0.5, friction = 0.5, density = 2.0 })
player:setReferencePoint( display.CenterReferencePoint )
movePlayer:insert(player)

ghost = display.newImage(“ghost1.png”, 26, 26 )
ghost.x = 300; ghost.y = 200
physics.addBody(ghost, “dynamic”, {bounce = 0.5, friction = 0.5, density = 2.0 })
ghost:setReferencePoint( display.CenterReferencePoint )
camera:insert(ghost)

–Move Display Group to follow player

function moveCamera()

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

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

end

Runtime:addEventListener(“enterFrame”, moveCamera)

–Create Function to Drag Player

function stopP()
player:setLinearVelocity(0,0)
end

function moveP ()

if t then timer.cancel(t)
end

– PLayer speed
vmax = 60

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 = 500 * (ds^0.5) / vmax

player:setLinearVelocity(velx, vely)

t = timer.performWithDelay(traveltime, stopP)

end

function l ( event )

if(event.phase == “moved”) then

eventX = event.x
eventY = event.y
moveP ()
end
end

camera:addEventListener(“touch”, l)[/lua]

[import]uid: 26289 topic_id: 12614 reply_id: 312614[/import]

In line 62 you are inserting the ghost in the “camera” layer. Try inserting it in the “movePlayer” layer instead. [import]uid: 9422 topic_id: 12614 reply_id: 46215[/import]

Thanks for reply. If i put the ghost in the same layer/display group then it moves along with the camera. It doesn’t stay put!

I’m moving to a Joystick method so I’m going to rewrite the code. Hopefully that will make a difference. Does anybody have any suggestions about making a camera follow a player, which is controlled by a Joystick. Exactly like Death Worm if anybody has played that. [import]uid: 26289 topic_id: 12614 reply_id: 46321[/import]

If you need help with a joystick , the code can be found here
http://developer.anscamobile.com/code/joystick [import]uid: 23546 topic_id: 12614 reply_id: 46329[/import]

Yeah I’ve grabbed that, thanks UniqDev. It is running perfect. Now I just need to work out how to get the camera/display group to follow my player. Do you have any ideas?

Here is my code

[lua]display.setStatusBar(display.HiddenStatusBar)
system.activate( “multitouch” )
camera = display.newGroup()
camera.x = 0
camera.y = 0

local player = require( “player” )
local joystickClass = require( “joystick” )

local function main()
backGround()
stick()
end
—Create Background Function
function backGround()
local bg1 = display.newImageRect( “bg1.png” , 480, 320 )
bg1:setReferencePoint( display.CenterLeftReferencePoint )
bg1.x = 0; bg1.y = 160
camera:insert(bg1)

local bg2 = display.newImageRect( “bg2.png” , 480, 320 )
bg2:setReferencePoint( display.CenterLeftReferencePoint )
bg2.x = 480; bg2.y = 160
camera:insert(bg2)

local bg3 = display.newImageRect( “bg3.png” , 480, 320 )
bg3:setReferencePoint( display.CenterLeftReferencePoint )
bg3.x = 0; bg3.y = 480
camera:insert(bg3)

local bg4 = display.newImageRect( “bg4.png” , 480, 320 )
bg4:setReferencePoint( display.CenterLeftReferencePoint )
bg4.x = 480; bg4.y = 480
camera:insert(bg4)
end

—Create Joystick Function and add Player
function stick()

– Add A New Joystick
joystick = joystickClass.newJoystick{
outerImage = “”, – Outer Image - Circular - Leave Empty For Default Vector
outerRadius = 60, – Outer Radius - Size Of Outer Joystick Element - The Limit
outerAlpha = 0, – Outer Alpha ( 0 - 1 )
innerImage = “joystickInner.png”, – Inner Image - Circular - Leave Empty For Default Vector
innerRadius = “”, – Inner Radius - Size Of Touchable Joystick Element
innerAlpha = 1, – Inner Alpha ( 0 - 1 )
backgroundImage = “joystickDial.png”, – Background Image
background_x = 0, – Background X Offset
background_y = 0, – Background Y Offset
backgroundAlpha = 1, – Background Alpha ( 0 - 1 )
position_x = 25, – X Position Top - From Left Of Screen - Positions Outer Image
position_y = 160, – Y Position - From Left Of Screen - Positions Outer Image
ghost = 155, – Set Alpha Of Touch Ghost ( 0 - 255 )
joystickAlpha = 0.4, – Joystick Alpha - ( 0 - 1 ) - Sets Alpha Of Entire Joystick Group
joystickFade = false, – Fade Effect ( true / false )
joystickFadeDelay = 2000, – Fade Effect Delay ( In MilliSeconds )
onMove = player.playerControl – Move Event
}
end

function moveCamera()

if(player.x > 20 and player.x < 460) then
camera.x = -player.x + 20
end

end

Runtime:addEventListener(“enterFrame”, moveCamera)

----Start Game
main() [/lua]

The moveCamera function isn’t working at all [import]uid: 26289 topic_id: 12614 reply_id: 46351[/import]

Haha now I’m back where I started!!! Although I have a cool joystick to show for my troubles. The problem now is that my player moves around the screen fine and the “camera” follows him round no problem but when I place another object in the group and give it physics then only the top left of the screen affects this object. My player has no effect on the object. I get the feeling this is something to do with the move camera function but i dunno [import]uid: 26289 topic_id: 12614 reply_id: 46354[/import]

Hi Cell,
I think is better use only a single group for all the physics objects (player, enemy, platforms…).
To move the camera I use this function (settings are valid for landscape orientation).
I’m not sure that it will fix your problem, but no harm in trying.

[code]
–> START

local x0 = player.x
local y0 = player.y

local function scrolling(e)
local playerContX, playerContY = player:localToContent(0,0);
local x1 = player.x
local y1 = player.y

if y1 > y0 and playerContY > 200 then
group1.y = group1.y - (playerContY - 200) -----
elseif y1 < y0 and playerContY < 120 then – note1: see at the end of code
group1.y = group1.y - (playerContY - 120) -----
else
group1.y = group1.y
end

if x1 > x0 and playerContX > 260 then
group1.x = group1.x - (playerContX - 260)-----
elseif x1 < x0 and playerContX < 220 then – note2: see at the end of code
group1.x = group1.x - (playerContX - 220)-----
else
group1.x = group1.x
end
y0 = y1
x0 = x1;
end

Runtime:addEventListener(“enterFrame”, scrolling)

–> END
[blockcode]

note1: if you replace all the “200” and “120” with “160”, the player will remain ever at the yCenter of the screen
note2: if you replace all the “260” and “220” with “240”, the player will remain ever at the xCenter of the screen

P.S. Someone can tell me how to get out from syntax highlighting? [import]uid: 70416 topic_id: 12614 reply_id: 46804[/import]

You have to use a closing tag to stop syntax highlighting. The only difference between closing tags and opening tags is a forward slash before the tag name so a closing lua tag would be [text][/lua][/text]. [import]uid: 27965 topic_id: 12614 reply_id: 46808[/import]

All the physics objects are required to be in the same group. No way around that. [import]uid: 35858 topic_id: 12614 reply_id: 46822[/import]

@calebr2048: tanks man! [import]uid: 70416 topic_id: 12614 reply_id: 46830[/import]

Thanks ivan-brogna and entertailion for the reply. I’ve already got it working though but cheers anyway. [import]uid: 26289 topic_id: 12614 reply_id: 46983[/import]