Jump to content

How do we reload a scene in Phaser 3


Baxter
 Share

Recommended Posts

It seems the restart method not clean events and registry. This is my way actually to restart a scene, whitout bubbling events and the registry

this.registry.destroy();
this.events.off();
this.scene.restart();

 

Edited by samme
format
Link to comment
Share on other sites

It's additionally worth noting `registry` is a globally shared object so even if you destroy the previous scene it won't clean up any data that you put in the registry. You'll still need to manually reset all of that.

If you want a scene-specific `DataManager` instance you can use this.data (remember though that access to it won't be trivially available to other scenes if you're using the registry to share information).

Link to comment
Share on other sites

Thanks, guys.

What is the paradigm here? I come from Libgdx where a scene (stage) can be reloaded simply by creating a new one.

For example, a scene can be used to display the in-game world or the HUD. Cleaning it and re-allocating the objects seems really error-prone.

Did I miss something?

Link to comment
Share on other sites

I wrote a thing about scenes which might fill in some of the gaps about how they can be used: https://github.com/jdotrjs/phaser-guides/blob/master/Basics/Part3.md. The Phaser.io newsletters I link to at the bottom go into more detail and are also great reading though, IMO, they're better as a sort of reference text than a guide.

tl;dr: super generally a scene is just a collection of things that should be displayed together in relation to other scenes and they doesn't imply any exclusivity, e.g., multiple scenes can be running at the same time. (I guess a caveat to that would be that an object can not belong to multiple scenes)

Link to comment
Share on other sites

Wow, thanks a lot!

From your tl;dr: If I understand correctly a scene is a wrapper. But how do we implement a life cycle doing something like that:

HomeScreen -> GameScreen -> GameOverScreen (and then goes back to home screen)

Without cleaning each field of the HomeScreen ? (In general gamedev something simple like currentScene = new HomeScreen() would perfectly do the job)

(Thanks again)

Link to comment
Share on other sites

See the scene tutorials: Part 1 Part 2.

A scene is an object responsible for creating, updating, and displaying its own game objects. It's instantiated only once, when it's added to the manager, but it can be started or stopped any number of times. When you stop a scene, all of its objects are destroyed, so you don't need to do that yourself.

The cycle you described is pretty similar to the one in https://github.com/samme/brunch-phaser/tree/master/app/scenes. You can move through scenes with scene.start(…), which stops the current scene and starts another.

Link to comment
Share on other sites

8 hours ago, Baxter said:

Wow, thanks a lot!

From your tl;dr: If I understand correctly a scene is a wrapper. But how do we implement a life cycle doing something like that:

HomeScreen -> GameScreen -> GameOverScreen (and then goes back to home screen)

Without cleaning each field of the HomeScreen ? (In general gamedev something simple like currentScene = new HomeScreen() would perfectly do the job)

(Thanks again)

Hey! that's a really good question! The answer is that you don't -- you need to explicitly initialize any state to the value that you want it to be set to at the beginning of a scene.

 

And if you don't want to deal with that you can do as samme suggested above and destroy-then-recreate the scene:

On 6/20/2018 at 9:50 AM, samme said:

Scenes are instantiated only once, so if you really want a new instance, you have to destroy the previous scene and add a new one.

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...