Moving a character on a level that's bigger than the screen

Hello Corona Community!

I would like to build a game where the level is bigger than the screen. However, I’m running into the issue of where the player does not collide with other physics objects.

In the code below, the player (using Peach’s awesome tutorials) stays in the middle of the screen with a simple d-pad in the bottom left corner for controls. I then placed three blocks in the level, one on screen and two off screen, and I want the player to stop when they collide with one of the blocks.

What can I do to make the player stop when they collide the blocks? I’ve tried setting the blocks to not allow sleeping, and that did not work. Help, please?

[code]
– Declare Game Variables
local motionx = 0
local motiony = 0
local speed = 2


– Insert Player and Controls

local player = display.newCircle (100,100,14)
player.x = _G.W/2
player.y = _G.H/2
physics.addBody(player, { density = 0, friction = 0, bounce = 0 })
player.isSleepingAllowed = false

local up = display.newCircle( 70, 230, 18)
local down = display.newCircle ( 70, 290, 18)
local left = display.newCircle ( 30, 260, 18 )
local right = display.newCircle ( 110, 260, 18)

local function stop (event)
– Stop movement when no arrow is pushed.
if event.phase ==“ended” then
motionx = 0
motiony = 0
end
end
Runtime:addEventListener(“touch”, stop )

local function moveGroups (event)
–player.x = player.x + motionx
–player.y = player.y + motiony
levelGroup.x = levelGroup.x - motionx
levelGroup.y = levelGroup.y - motiony
end
Runtime:addEventListener(“enterFrame”, moveGroups)

function up:touch()
motionx = 0
motiony = -speed
end
up:addEventListener(“touch”, up)

function down:touch()
motionx = 0
motiony = speed
end
down:addEventListener(“touch”, down)

function left:touch()
motionx = -speed
motiony = 0
end
left:addEventListener(“touch”,left)

function right:touch()
motionx = speed
motiony = 0
end
right:addEventListener(“touch”,right)

– END Insert Player and Controls


– Start Level Creation

levelGroup = display.newGroup();

block = display.newRect (100,100,100,100)
block.x = 100; block.y = 100;
physics.addBody( block, “dynamic”, { } );
block.isSleepingAllowed = false
levelGroup:insert(block);

block2 = display.newRect (100,100,100,100)
block2.x = -25; block2.y = 100;
physics.addBody( block2, “dynamic”, { } );
block2.isSleepingAllowed = false
levelGroup:insert(block2);

block3 = display.newRect (100,100,100,100)
block3.x = -150; block3.y = 100;
physics.addBody( block3, “dynamic”, { } );
block3.isSleepingAllowed = false
levelGroup:insert(block3);

– END Level Creation

[/code] [import]uid: 14218 topic_id: 31383 reply_id: 331383[/import]

Grouping issue; physics objects need to be in the same group in order to collide properly, basically. Your player isn’t in the levelGroup you have created so that is where the issue is coming from - need to group together/not group at all. [import]uid: 52491 topic_id: 31383 reply_id: 125501[/import]

Thank you Peach! Now that the player is colliding with the obstacles, what can I do to make the character stay in the center of the screen? [import]uid: 14218 topic_id: 31383 reply_id: 125502[/import]

Hi Daniel,
You can reset your player to the screen-relation center during your moveGroups Runtime listener. You just need to calculate that based on the coordinate difference between the levelGroup and 0,0. So, for example, if your levelGroup and every object inside it shifts to -10,0 (left of its default by 10 pixels), you need to adjust your player to the right by 10 pixels to compensate. Subtract the difference on every frame and then add that to your player.x (or y). For example:

  1. levelGroup begins at 0,0
  2. by movement, levelGroup (and player within it) shifts to -10, 5
  3. x difference: 0 - -10 = 10
  4. player.x = player.x + 10
  5. y difference: 0 - 5 = -5
  6. player.y = player.y + -5

This formula will work in any direction: up, down, left, right, diagonal, whatever.

Best of luck!
Brent
[import]uid: 9747 topic_id: 31383 reply_id: 125531[/import]

Grouping issue; physics objects need to be in the same group in order to collide properly, basically. Your player isn’t in the levelGroup you have created so that is where the issue is coming from - need to group together/not group at all. [import]uid: 52491 topic_id: 31383 reply_id: 125501[/import]

Thank you Peach! Now that the player is colliding with the obstacles, what can I do to make the character stay in the center of the screen? [import]uid: 14218 topic_id: 31383 reply_id: 125502[/import]

Hi Daniel,
You can reset your player to the screen-relation center during your moveGroups Runtime listener. You just need to calculate that based on the coordinate difference between the levelGroup and 0,0. So, for example, if your levelGroup and every object inside it shifts to -10,0 (left of its default by 10 pixels), you need to adjust your player to the right by 10 pixels to compensate. Subtract the difference on every frame and then add that to your player.x (or y). For example:

  1. levelGroup begins at 0,0
  2. by movement, levelGroup (and player within it) shifts to -10, 5
  3. x difference: 0 - -10 = 10
  4. player.x = player.x + 10
  5. y difference: 0 - 5 = -5
  6. player.y = player.y + -5

This formula will work in any direction: up, down, left, right, diagonal, whatever.

Best of luck!
Brent
[import]uid: 9747 topic_id: 31383 reply_id: 125531[/import]