Loading External Scenes

Demonstrates how to load an external JavaScript file containing a Scene class and dynamically add multiple instances of that scene to the game manager at runtime.

Loading External Scenes, running
1024 × 768
GitHub
class Controller extends Phaser.Scene
{
    constructor ()
    {
        super({ key: 'controller' });
    }

    preload ()
    {
        this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
        this.load.script('demo', 'src/scenes/external scene/Demo.js');
    }

    create ()
    {
        var clone = 0;

        this.time.addEvent({ delay: 2000, callback: function () {

            this.scene.add('demo' + clone, Demo, true);
            clone++;

        }, callbackScope: this, repeat: 2 });
    }
}

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

const game = new Phaser.Game(config);

More in Scenes52 examples