onCollision event?

Greetings all,

I am trying to figure out how to trigger a conditional event based on a collision happening between the player and another object. I’ve been trying to go through the documentation to understand how I might go about this, but, the documentation hasn’t been super helpful for me… I’ve also looked through the examples and i can’t seem to find anything there either (maybe I am blind?)

This is basically how I need the logic to go down

if (playerCollidesOrOverlapsWithObject)
{
  isTouching = true;
}else
{
  isTouching = false;
}

so that I may do this

if (isTouching == true)
{
  run code for this particular collision
}

Thanks you in advance and I apologize for such a newbie question. :slight_smile:

Are you using a physics engine in your game? Arcade, MatterJs, Impact?

If so, you can find the example here

If you are not using physics, and you just want to find the overlap of two objects you can use something like:

checkCollide(obj1, obj2) {
        var distX = Math.abs(obj1.x - obj2.x);
        var distY = Math.abs(obj1.y - obj2.y);
        if (distX < obj1.width / 2) {
            if (distY < obj1.height / 2) {
                return true;
            }
        }
        return false;
    }

That function assumes a display object with an origin set at .5. sprite.setOrigin(0.5,0.5)Preformatted text

You can use Overlaps method.

There is also Intersection method.

sprite.getBounds() returns Phaser.Geom.Rectangle object. You can pass that object to any of these methods.