Custom Image Class

Learn how to extend the Phaser Image class to create your own custom game object types and add them to the Scene.

Custom Image Class, running
1024 × 768
GitHub
class Example extends Phaser.Scene
{
    preload ()
    {
        this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
        this.load.image('contra', 'assets/pics/contra3.png');
    }

    create ()
    {
        this.children.add(new EnemyRobot(this, 264, 250));
        this.children.add(new EnemyRobot(this, 464, 350));
        this.children.add(new EnemyRobot(this, 664, 450));
    }
}

class EnemyRobot extends Phaser.GameObjects.Image
{
    constructor (scene, x, y)
    {
        super(scene);

        this.setTexture('contra');
        this.setPosition(x, y);
        this.setScale(2);
    }
}

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

const game = new Phaser.Game(config);

More in Images16 examples