Circle Hit Area

Learn how to define a circular interactive hit area for a Sprite using a Geometric Circle shape and custom input callbacks.

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

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

        //  The circle x/y relates to the top-left of the sprite.
        //  So if you want the circle positioned in the middle then you need to offset it by half the sprite width/height:
        const shape = new Phaser.Geom.Circle(46, 45, 45);

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

        //  Input Event listeners

        sprite.on('pointerover', () =>
        {

            sprite.setTint(0x7878ff);

        });

        sprite.on('pointerout', () =>
        {

            sprite.clearTint();

        });
    }
}

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

const game = new Phaser.Game(config);

More in Mouse25 examples