Moving player + scrolling background + obstacle = problem

It seemed easy enough to fix but after 2 days, I need help!
So here’s the situation, simple platform with a player.

  1. Player can move left/right.
  2. Background scroll left/right at the same speed of Player.
  3. Player and BG in a group called bgGroup.
  4. To keep player centered while moving, as Player is moving, bgGroup moves in opposite direction so simulate the background scrolling while Player stays centered.

All is good until Player meets an obstacle and can’t forward anymore. bgGroup is still moving since button’s pressed but Player is stuck and Player moves away from the center and disappears to side.

Here’s what I tried:

  • detect change in x position of player within two different times. Sort of works but every time I click and let go of D-pad, Player does move little by little.
  • use velocity info of Player. For some reason, velocity is always 0 even if Player is moving (may because I am using x position to move Player?) [import]uid: 39031 topic_id: 15550 reply_id: 315550[/import]

Velocity will be 0 unless you are applying velocity.

I’m having trouble picturing your app; is your player constantly centered? Or is he able to move left and right on the screen? (Normally when I see a scrolling background the player keeps the same X position throughout the game.)

I know you say that the player can move left and right but I’m not sure if you mean that literally or not.

Can you give a little more info, please?

Peach :slight_smile: [import]uid: 52491 topic_id: 15550 reply_id: 57467[/import]

Hi, Peach!
Before I go any further, thanks for being a great resource and providing wonderful tutorials. I would be lost without those :slight_smile:
Anyway, I guess it would be better to post some codes to make things easier. It is just like Mario type platform.
Here are some facts.

  1. Stage is 2X screen width.
  2. There are boxes (no shown in this code) on stage.
  3. Boxes, bg, and the player are all in the same group (bgGroup).
  4. Player can interact with the boxes (collide, jump on, etc)
  5. Once the player moves to 1/2 x position (moving from left to right) on screen, bgGroup starts moving opposite direction (left) and the player continue moving right but visually it stays put in the center because bgGroup is moving left.

I am wondering whether I am approaching this correctly and if so, how can I keep the background from scrolling and the player stay put in center, when the player encounters a static box and can’t walk further but d-pad is being pressed (touched)?

[code]

local playerRight = 0
local playerLeft = 0
local playerSpeed = 2
local motionPlayer = 0
local motionBg = 0

– actions
local function movePlayerRight(e)
if playerRight == 1 then
if player.x < _W * 0.5 then
motionPlayer = playerSpeed
motionBg = 0
elseif player.x > _W * 0.5 and bgGroup.x > -_W then
motionPlayer = playerSpeed
motionBg = -playerSpeed

else
motionPlayer = playerSpeed
motionBg = 0
end
player.x = player.x + motionPlayer
bgGroup.x = bgGroup.x + motionBg
end
– don’t let it go past right wall
if player.x >= _W * 2 then
player.x = _W * 2
end
end

local function movePlayerLeft(e)
if playerLeft == 1 then
if player.x > (_W * 2) - _W * 0.5 then
motionPlayer = playerSpeed
motionBg = 0
elseif player.x < (_W * 2) - _W * 0.5 and player.x > _W * 0.5 then

motionPlayer = playerSpeed
motionBg = -playerSpeed
else
motionPlayer = playerSpeed
motionBg = 0
end
player.x = player.x - motionPlayer
bgGroup.x = bgGroup.x - motionBg
end
– don’t let it go past left wall
if player.x <= 0 then
player.x = 0
end
end

local function EnterFrame(e)
movePlayerRight(e)
movePlayerLeft(e)
end
Runtime:addEventListener( “enterFrame” , EnterFrame )

function goRight(e)
if(e.phase == “began”) then
playerRight = 1
elseif(e.phase == “ended” or e.phase == “cancelled”) then
playerRight = 0
end
return true
end

function goLeft(e)
if(e.phase == “began”) then
playerLeft = 1
elseif(e.phase == “ended” or e.phase == “cancelled”) then
playerLeft = 0
end
return true
end
[/code] [import]uid: 39031 topic_id: 15550 reply_id: 57504[/import]

Please help! [import]uid: 39031 topic_id: 15550 reply_id: 57668[/import]

Hey there,

Thanks for the kind words :slight_smile:

I am sorry you have had to wait a bit but sadly due to time differences I can’t always be here :wink:

How, the code above is not something I can test with; are you able to upload your project somewhere so I can take a look, or alternatively provide some plug and play code?

Peach :slight_smile: [import]uid: 52491 topic_id: 15550 reply_id: 57724[/import]

Of course! Totally understandable.
Anyway, I have placed the files in the location below.
http://www.berrymobi.com/upload/testfiles.zip

Once you run it and move the red box toward the obstacle and hit it you will immediately see the problem. I want everything to stop on track once the red box hits the obstacle.
Only way I can think of is to measure before and after x position in a given amount of time, and if there is a change, the box is moving, if not, the box is not moving and bgGroup should stop moving as well. But, that didn’t work like I hoped.
Any idea would be appreciated.
Thanks!

Doug [import]uid: 39031 topic_id: 15550 reply_id: 57868[/import]

Hey Doug,

Well written code you’ve got there - very neat, too :slight_smile:

I have taken a look and it seems to me that the best way to handle this would be to have a collision listener on your player so that when it collides with the block, if it’s X is < block.x and it’s Y is NOT > block.y then you stop the background moving, resuming it once collision ends*.

*My reason for this thinking is that if your player can jump, collision will end then. If the player jumps straight up and down when they try to move forward again and collide with the block, it will once again stop the BG from moving.

Does this make sense?

Peach [import]uid: 52491 topic_id: 15550 reply_id: 57947[/import]

Wow, I am always conscious about my code being horrible :slight_smile:

I thought about using collision but that would be problematic since there is a constant collision between the floor and player.
I suppose I can go around this by naming the floor object and do an exclusion for it. But, I am planning to use a lot of floating platforms with obstacles on them and it would make things quite complicated if I had to do that for every single one of them.

I also tried using x position. Measure x position now and say, 50ms later using a timer, and if touch button’s depressed while this is happening, then stop everything. I kinda got this to work half way but if I repeat going left and to right again hitting the obstacle, the player+bg move several pixels each time before everything stops, eventually putting the player out of the screen.

It’s frustrating because virtually every platform games out there have this happening and I was expecting to find a sample easily but I couldn’t, and it seems so simple in concept as well… [import]uid: 39031 topic_id: 15550 reply_id: 57999[/import]

Hmmmm.

Well, what about making a sensor on the right of the player?

You could position it so it didn’t touch the ground.

This way when it collided with anything solid you’d know to stop the background moving.

Perhaps that would work better than the previous suggestion of actually checking X and Y?

Peach :slight_smile: [import]uid: 52491 topic_id: 15550 reply_id: 58155[/import]

Hi, Peach,

I added two sensors (left and right) and they seem to work pretty nicely now.

Thank you for all your help!

Doug [import]uid: 39031 topic_id: 15550 reply_id: 58296[/import]

I’m VERY happy to hear that :smiley: (It’s great when things finally work after a bit of poking and prodding, eh?)

Peach :slight_smile: [import]uid: 52491 topic_id: 15550 reply_id: 58339[/import]