Triangle Hit Area

Demonstrates how to define a custom triangular hit area for a sprite using a geometric shape and a custom callback.

Triangle Hit Area, running
800 × 600
GitHub
class Example extends Phaser.Scene
{
    preload ()
    {
        this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
        this.load.image('ship', 'assets/sprites/thrust_ship2.png');
    }

    create ()
    {
        const sprite = this.add.sprite(400, 300, 'ship').setScale(8);

        const shape = new Phaser.Geom.Triangle.BuildEquilateral(16, 0, 30);

        sprite.setInteractive(shape, Phaser.Geom.Triangle.Contains);

        //  Input Event listeners

        sprite.on('pointerover', function ()
        {

            this.setTint(0x7878ff);

        });

        sprite.on('pointerout', function ()
        {

            this.clearTint();

        });
    }
}

const config = {
    scale: { mode: Phaser.Scale.FIT },
    type: Phaser.WEBGL,
    parent: 'game-container',
    pixelArt: true,
    width: 800,
    height: 600,
    scene: Example
};

const game = new Phaser.Game(config);

More in Mouse25 examples