Export Sprite to JSON

Demonstrates how to use the toJSON method to export a Sprite's current state—including position, rotation, scale, and alpha—into a serializable JSON object.

Export Sprite to JSON, running
800 × 600
GitHub
class Example extends Phaser.Scene
{
    preload ()
    {
        this.load.setBaseURL('https://cdn.phaserfiles.com/v410');
        this.load.image('bunny', 'assets/sprites/bunny.png');
    }

    create ()
    {
        const bunny = this.add.sprite(400, 300, 'bunny');

        //  Set a few properties:

        bunny.angle = 25;

        bunny.setScale(1.3);

        bunny.alpha = 0.9;

        bunny.flipY = true;

        console.log(bunny.toJSON());

        // console.log(JSON.stringify(bunny));

    }
}

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

const game = new Phaser.Game(config);

More in Sprites9 examples