Swapping Scenes

Demonstrates how to switch between two different scenes using the scene.switch method, which puts the current scene to sleep and starts or wakes the target scene on user interaction.

Swapping Scenes, running
800 × 600
GitHub

const sceneConfigA = {
    key: 'sceneA',
    create: createA,
    pack: {
        files: [
            { type: 'image', key: 'face', url: 'assets/pics/bw-face.png' }
        ]
    }
};

const sceneConfigB = {
    key: 'sceneB',
    create: createB,
    pack: {
        files: [
            { type: 'image', key: 'logo', url: 'assets/pics/monika-krawinkel-amberstar-title.png' }
        ]
    }
};

function createA ()
{
    this.add.image(400, 300, 'face');

    this.input.on('pointerdown', () =>
    {
        this.input.stopPropagation();
        this.scene.switch('sceneB');
    });
}

function createB ()
{
    this.add.image(400, 300, 'logo');

    this.input.on('pointerdown', () =>
    {
        this.input.stopPropagation();
        this.scene.switch('sceneA');
    });
}

const gameConfig = {
    scale: { mode: Phaser.Scale.FIT },
    type: Phaser.CANVAS,
    parent: 'game-container',
    width: 800,
    height: 600,
    scene: [ sceneConfigA, sceneConfigB ]
};

const game = new Phaser.Game(gameConfig);

More in Scenes52 examples