Wall not stopping moving player.

Hey guys, I am trying to create basic player movement and it is working! However, if you click the arrow key multiple times at once the player can go “inside” the wall. Then if you press it again, it move out. Any help would be greatly appreciated! Code below. Regards

Player Creation

 player = display.newImage("images/player.png", 100, 100) physics.addBody(player, "dynamic", {friction=0, bounce=0.9 }) 

Wall Creation

function spawnWall(x,y) local wall= display.newImage("images/wall.png", x, y) physics.addBody(wall, "static",staticMaterial) end 

Player Movement

function moveUp(event) if event.phase == "ended" then player.y = player.y - 100 end end function moveDown(event) if event.phase == "ended" then player.y = player.y + 100 end end function moveLeft(event) if event.phase == "ended" then player.x = player.x - 100 end end function moveRight(event) if event.phase == "ended" then player.x = player.x + 100 end end

The problem is that when you add a physics body to an object, you cannot move it the same way you would move a regular display object (i.e doing pplayer.x = player.x + 100).

If you do that, the display object will move, but the physics body will stay in the same place and so the physics bodies from the player and the wall will never collide. 

Instead you need to do something like:

function moveRight(event) if event.phase == "ended" then player:applyForce( 100, 0, player.x, player.y ) end 

Or attach a joint, call setTarget on the joint and then remove it.

There are a few options available to you, I would advise looking up the physics docs and some physics posts on the forums.

Hey, thanks for the help, however the player continues to move. I want to the player to move 100 pixels in one direction then stop. I am sure you understand. Would it be possible to attach an invisible box to the player that moves around, detecting collisions?

When making that invisible collision detecting box, what exactly would I attach together with the joint? Also, what type of joint?

I think it’s called a weld joint.

The problem is that when you add a physics body to an object, you cannot move it the same way you would move a regular display object (i.e doing pplayer.x = player.x + 100).

If you do that, the display object will move, but the physics body will stay in the same place and so the physics bodies from the player and the wall will never collide. 

Instead you need to do something like:

function moveRight(event) if event.phase == "ended" then player:applyForce( 100, 0, player.x, player.y ) end 

Or attach a joint, call setTarget on the joint and then remove it.

There are a few options available to you, I would advise looking up the physics docs and some physics posts on the forums.

Hey, thanks for the help, however the player continues to move. I want to the player to move 100 pixels in one direction then stop. I am sure you understand. Would it be possible to attach an invisible box to the player that moves around, detecting collisions?

When making that invisible collision detecting box, what exactly would I attach together with the joint? Also, what type of joint?

I think it’s called a weld joint.