Managing scenes... so confusing to me!

Hi!

I am experiencing some difficulty in the management of scenes particularly in THIS situation…

I have 4 scenes:

sceneInit
sceneHelp
scenePlay
sceneMenu

Where “sceneInit” is the main title. This scene has a “play” and a “help” button that supposed to move to scenePlay or sceneHelp respectively. This is the code I have:

// help button -> pops up sceneHelp
this.scene.start('sceneHelp');
// play button -> move to scenePlay
this.scene.switch('scenePlay');

In the sceneHelp I have a “close” button:

this.scene.setVisible(false);

It works fine.

However, in the scenePlay I have a “menu” button that pops the menuScene:

game.scene.start("sceneMenu");

The issue happens in the sceneMenu where I have a “init” button that supposed to bring player back to the sceneInit. My code for this button:

game.scene.start("sceneInit");

Instead to move back to the sceneInit, however, it will just close the sceneMenu and will get back to the scenePlay BUT regardless the scenePlay is being shown no one of those buttons works. In fact it seems that I can click the buttons of the sceneInit that looks like to be under scenePlay.

My questions:

  1. What am I doing wrong here? (I have the feeling that I am using the scene managing methods kind of randomly)
  2. When should I use start, restart or switch?
  3. When I move to a different scene what should I do with the current one: setVisible(false) or destroy or what?

Thanks!

I use

for these kinds of problems.

Don’t use scene.setVisible(false).

Don’t use game.scene. Use this.scene instead.

Generally, you want to first figure out how to enter/exit scenes and then choose the right methods depending on the scene flow.

My guess is that sceneInit, sceneHelp, and sceneMenu should be started, then slept, then awakened. scenePlay should be started, then stopped, then started (for replaying); or slept and awakened (when opening and closing the help screen).

2 Likes

Samme,

I figured this out. I was messing up things. Actually TWO specific things:

  1. Sometimes I was using game.scene and other times using this.scene. In fact YOU already had warned me about not to use game.scene in a couple of my posts. My bad.

  2. In some scenes I was using regular functions in the click event of the buttons to start the scenes and in others I was using arrow functions.

The solution was indeed to change everything to this.scene and move everything to arrow functions. Now it is working as supposed.

PS: I will check your links to get more secure about using scenes though.

Thanks once more!

1 Like

HOWEVER! I just realized that it caused me another problem. Now when I start a popup over a scene, my background scene disappear. I figured that if I use launch instead start I can open a scene over a scene:

  this.btnLaunchPopup.on('pointerup', (e) => {
            this.scene.launch("scenePopup");
        });

But then if I try to start another scene from the popup (launched scene) it will just close the popup but won’t change to the desired scene:

In the popup:

  this.btnGoInit.on('pointerup', (e) => {
            this.scene.start("sceneInit");
        });

It will only close the launched scene but won’t go to the scene.

Any idea?

I hope you are using the plugin. Without it it’s a lot of guesswork. :slight_smile:

Remember that scene.start('key') is a double operation: it stops the calling scene and starts the named scene.

Whatever scene is visible but shouldn’t be, you need to sleep or stop it.

2 Likes

I got it now!!! Thanks for the clarification! This is the fix that worked for me:

this.btnGoInit.on('pointerup', (e) => {
            this.scene.stop("sceneMain"); // the scene which launched the popup
            this.scene.start("sceneInit");
        });

Thanks again! :slight_smile: