Loading and Retrieving Text Files

Learn how to load external text files into the Phaser Cache and retrieve their raw string content for use in your game.

Loading and Retrieving Text Files, running
800 × 600
GitHub
class Example extends Phaser.Scene
{
    constructor ()
    {
        super();
    }

    preload ()
    {
        this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
        this.load.image('logo', 'assets/sprites/phaser.png');
        this.load.text('data', 'assets/loader-tests/test.txt');
    }

    create ()
    {
        this.add.image(8, 8, 'logo').setOrigin(0);

        const data = this.cache.text.get('data');

        this.add.dom(400, 300, 'div', 'background-color: rgba(0, 0, 80); width: 600px; height: 500px; font: 12px Courier; color: white; overflow: hidden', data);
    }
}

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

const game = new Phaser.Game(config);

More in Cache3 examples