Custom Sprite Class

Demonstrates how to extend the Sprite class using ES6 syntax to create custom game objects with their own internal logic and preUpdate lifecycle methods.

Custom Sprite Class, running
1024 × 768
GitHub
class Brain extends Phaser.GameObjects.Sprite {

    constructor (scene, x, y)
    {
        super(scene, x, y);

        this.setTexture('brain');
        this.setPosition(x, y);
    }

    preUpdate (time, delta)
    {
        super.preUpdate(time, delta);

        this.rotation += 0.01;
    }

}

var config = {
    scale: { mode: Phaser.Scale.FIT },
    type: Phaser.AUTO,
    parent: 'game-container',
    scene: {
        preload: preload,
        create: create
    }
};

var game = new Phaser.Game(config);

function preload ()
{
        this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
    this.load.image('brain', 'assets/sprites/brain.png');
}

function create ()
{
    this.add.existing(new Brain(this, 264, 250));
    this.add.existing(new Brain(this, 464, 350));
    this.add.existing(new Brain(this, 664, 450));
}

More in Sprites9 examples