Issue transitioning between scenes with a button

Hey everyone,

I have an issue transitioning between scenes after a click on a button. Here’s my code :

    create(){
    let myButton = this.add.text(20,160,'CLICK HERE',{font : '18px'})
    myButton.setInteractive();
    myButton.on('pointerdown', () => {
        this.scene.start('final')
    });
    }

I quit the current scene but only get a black scene with nothing inside. Even a simple console.log in the “final” scene is missing and I get no error at all. After different tests, it looks like the button is guilty here, but I can’t understand why and have no idea how to solve the issue.

Many thanks for your help!

Is your scene properly registered in your index.html (if it’s a separate js file), in your game’s config scene: [] array? Can you check if the scene loads by itself?

It is! I have three scenes. I can easily transition between the first and the second one (I use a simple setTimeout in that case). If I change the setTimeout for a button click, I get the exact same issue. This makes me think scenes are well defined.

check who is “this” maybe you hold “scene” and not the “game”

“This” is the scene. But it looks legit to me if I read this guide which stresses each Scene is a full world.

1 Like

you need to bind it to this

myButton.on('pointerdown', () => { this.scene.start('final') }, this);

check if that works :slight_smile:

Arrow functions do not have a scope of their own, so providing one in the event listener is not necessary. What’s more, if this was a problem with the function’s scope, you would probably have gotten an error. If this were the game instance, the old scene would not be stopped.

I don’t know what’s causing this. Changing the scene from a pointerdown event works fine in the Changing Scene example, even if it’s modified with the exact code you provided. Can you try reproducing this issue outside of your game (for instance, in an example)?

You can run game.scene.getScene('final').sys.isActive() from your browser’s console (replace game with a reference to your Phaser.Game instance) to check if the scene is running. The value of game.scene.getScene('final').sys.settings.status could also help determine what the scene is doing. By the way, which version of Phaser are you using? A problem with the particular version might be causing this, even though I can’t remember any issues related to this. I’ve tested it with 3.17 (the version in the example runner) and 3.19.

2 Likes

Aaaaaaaaaaand… I was working on the wrong file. Before I hang myself up for stupidity, please let me thank everybody for your help. I’ve learned to use “.sys.settings.status” and “.sys.settings.status” in the process. :slight_smile:

1 Like