How to Restart a Scene?

Hi, I have a “PlayScene” that is my main game which I start fro the “BootScene” with this code:

this.scene.start(PLAY_SCENE)

Then when the player dies I have a “GameOverScene”, and when the player clicks the “Play again” button I would like to restart the play scene from the beginning.

The issue is that when I try to start the play scene again… nothing happens. I am just left with a dark grey screen…

1 Like

maybe you can show that some of related code.
Reastart can be simply achive by this.scene.restart(), or if you was start to the other scene (A to B), it can be start the scene A again.

Hey @ReydVires!

thanks, I am trying to start my “PLAY_SCENE” from the “GAME_OVER_SCNENE” which launches on top of the play scene.

this.scene.launch(GAME_OVER_SCENE)

window.setTimeout(() => {

    console.log('starting play scene!')

        this.scene.start(PLAY_SCENE)

    }, 3000)

}

then in the gameover scene:

The play scene does reload, but for some reason the mouse listeners stop working after I start the scene a second time… :thinking:

Any ideas why the mouse listeners stop working here?

It is an open-source project with the full code here: https://github.com/EverybodyCodes/Phaser-Fish-Game/tree/gh-pages

Thanks!

I can’t find any code on your GitHub, only one unreadable file starting with app. Is it somewhere else?

Hey @UltimateProGrammer!

Sorry, that link was just the gh-pages branch. You can find the full source code on the master branch here: https://github.com/EverybodyCodes/Phaser-Fish-Game/tree/master

This might not help but what mouse listeners are you using?

I am not even setting up any listeners. I’m just using the scene’s built-in “activePointer”:

this.pointer = this.input.activePointer

and then reading that in the “update” function.

Try using event listeners instead. I know its a pain to switch over but you might as well give it a shot.

edit:
Before switching over all of your code, just try it out first. Here’s some example code:

this.input.on('pointerdown', function(pointer) {
    console.log(pointer.x);
    console.log(pointer.y);
}

Thanks!

I tried setting my pointer like that instead:

this.input.on('pointerdown', (pointer: any) => {
  this.pointer = pointer
});

However, this errors when I try to get the pointer position relative to the camera :cry:

const lockedToCamPointer = this.pointer.positionToCamera(this.cameras.main)

The scene then crashes with the error:

“Uncaught TypeError: Cannot read property ‘positionToCamera’ of undefined”

I normally just add this.cameras.main.scrollX and Y to account for camera scrolling.

Can you just put your pointer code inside of the event listener?

That error is probably happening because you are setting the const before the pointerdown event has happened.

Thanks!

I pushed some changed to the code. I am still using the update function, but I changed the code to use the listener instead.

It works when I start the scene the first time, but unfortunately I am getting really crazy values after I start the game again from the game over screen. :confused:

Is there anything like a “destroy” function that I can hook into? Maybe I need to manually remove the listeners when the scene is removed? :thinking:

I don’t think that would work but there is a remove() function for listeners. Look it up on the docs

I will look at your code later when I have more time to see if I can help with more specifics

1 Like

Thanks! Please check it out as I am totally baffled.

Also, I tried searching for “remove” in the docs, but it brought up a list of one million unrelated classes. How do you recommend using the docs to find this mystery remove function? It doesn’t exist on the scene where I would think it should be. :thinking:

My bad. It’s called removeListener. I’ve never used it before and it I’m pretty certain it won’t work but here is the documentation: https://photonstorm.github.io/phaser3-docs/Phaser.Events.EventEmitter.html#removeListener

Okay I have a question about your goal for the pointer. Are you trying to get the camera to follow the pointer or just have the pointer’s x and y relative to the camera?

I would like to have the pointer’s x and y be relative to the camera.

It works perfectly when it loads up the first time. It’s just when you die and it tries to restart the Play scene… everything breaks.

Sometimes just because it works doesn’t mean its right. I’ll tell you my suggestions once I’ve fully formed them :).

Do you want the fish to be constantly moving towards the pointer? I will assume so as you want it to be similar to agar.io