Event Arguments

Learn how to pass and receive multiple parameters when emitting custom events in Phaser.

Event Arguments, running
800 × 600
GitHub
class Example extends Phaser.Scene
{
    preload ()
    {
        this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
        this.load.image('plush', 'assets/pics/profil-sad-plush.png');
    }

    create ()
    {
        this.events.on('addImage', this.handler, this);

        //  The handler function will be sent 2 arguments: x and y

        this.events.emit('addImage', 200, 300);
        this.events.emit('addImage', 400, 300);
        this.events.emit('addImage', 600, 300);
    }

    handler (x, y)
    {
        this.add.image(x, y, 'plush');
    }
}

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

const game = new Phaser.Game(config);

More in Events9 examples