Jump to content

Sprite vs Sprite overlap - how to tell if you're landing from the top or from the side


GooseZA
 Share

Recommended Posts

Hi all, 

 

Making a basic mario clone and busy working on killing the enemies when the player jumps on top of them. If I use physics.collide it works great with the following in my collideCallback

 collideWithEnemy: function (player, enemy) {            if (player.body.touching.down && !player.body.onFloor()) {                player.body.velocity.y = -400;                enemy.killedByPlayer();            } else {                player.alive = false;                player.body.velocity.x = 0;                player.body.velocity.y = -400;                player.outOfBoundsKill = true;                player.body.collideWorldBounds = false;            }         }

If I land on top then the enemy dies, otherwise if the collision is from the side I die. The problem is that the collision causes the enemy to stop moving and I want him to keep walking. 

 

I'm trying to use overlap to solve this as it doesn't apply physics to either of the objects. How would I check if I'm landing mostly from the top in order to kill the enemy?

 

I've thought about player.body.y + height > enemy.body.y but that would be true for every overlap.

 

Thanks in advance =)

Link to comment
Share on other sites

I think you are on the right track with using collide and then do you own checks to figure out what kind of a collide (kill player or kill enemy).

 

How about something like this:

if ( player.body.y + player.body.height < enemie.body.y + 20) {  enemie.kill() } else {  player.kill();}

If the players feet (y + height) are within the head of the enemie (the top most 20 pixel of the enemie) then we kill the enemie, otherwhise we kill the player.

Link to comment
Share on other sites

I think you are on the right track with using collide and then do you own checks to figure out what kind of a collide (kill player or kill enemy).

 

How about something like this:

if ( player.body.y + player.body.height < enemie.body.y + 20) {  enemie.kill() } else {  player.kill();}

If the players feet (y + height) are within the head of the enemie (the top most 20 pixel of the enemie) then we kill the enemie, otherwhise we kill the player.

 

That sounds like a step in the right direction. Will give it a go and see how it turns out :D

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...