Kacim Fedjkhi
Kacim Fedjkhi

Reputation: 143

How do I pass data from scene to scene in phaser 3?

I'm making a game in Phaser 3 but I can't seem to find how to pass the score from a GameScene to a GameOverScene.

Upvotes: 13

Views: 10363

Answers (1)

James Skemp
James Skemp

Reputation: 8571

When calling this.scene.start you can pass optional data to the scene.

this.scene.start(key, data), which has an official demo.

You can use the init in a scene to retrieve the data.

So in your GameScene you might have something like the following:

this.scene.start('GameOverScene', { score: this.playerScore });

Then in your GameOverScene you should have something like the following:

init: function (data)
{
    console.log('init', data);
    this.finalScore = data.score;
}

Upvotes: 16

Related Questions