How to load scenes correctly?

I am new to Phaser and I’m not sure I’m handling loading scenes correctly.

I have a LoadScene where once it reaches create() I send it to load a MenuScene by using

this.scene.start(STATIC.SCENES.MENU, { data: 0 });

Ignore the {…] that was for some testing.

From that Menu Scene, I have 3 buttons to “Play”, “Settings” and “Credits”

I load those on mouse and keyboard inputs with

this.scene.start(this.scenes[index]);

“scenes” is just an array with the keys for each scene

this.scenes = [STATIC.SCENES.PLAY, STATIC.SCENES.SETTINGS, STATIC.SCENES.CREDITS];

Finally on this scenes I have for now just a “back” button that is loading the Menu Scene again with

this.scene.start(STATIC.SCENES.MENU);

As far as I know from what I read is that using start method stops the current scene we are on. However, when I go back to the main menu, something weird happens.

My buttons for “Play”, “Settings” and “Credit” play a sound on mouse over and when selected with the arrows.

On menu first load the sound effect volume is perfect, however after coming back to that scene from the back button on the others, when using the arrows to select the menu options, the sound effect sounds louder and louder (Which makes me think, I’m loading scenes on top of scenes).

While I’m on Play, Settings or Credits, up and down arrows don’t play any effect, so clearly the menu scene isn’t on the back.
Weirdly enough (or maybe not) when on mouse over, the volume is fine. So I guess my:

this.input.keyboard.on('keydown', (event: any) => {

Is still running and I’m just adding another listener on top?

Anyone has any idea what could be going on?

Let me know if you need any more of the code.

Hi, just as an idea… maybe for moving between menus is a better idea to use this.scene.switch() as I run with a similar issue testing something like this and I ended using “switch”.
If not, would be nice if someone more used to work with menus or with better knowledge in scene best practices bring some light to this…

You can check

console.log(this.input.keyboard.listenerCount('keydown'));

before adding listeners.

Using switch solved the problem. Thanks

I’m actually curious what was wrong. The scene removes its keyboard event listeners when shut down (before restarting), so I don’t think those should stack up.

I don’t think they were stacking neither, and the console didn’t show duplicate responses. I wonder if someone can reproduce it to confirm the weird behavior and understand better where the problem comes from.