# How to Restart a Scene?

**URL:** <https://phaser.discourse.group/t/how-to-restart-a-scene/5819>\
**Category:** Phaser 3\
**Created:** [April 8, 2020, 3:31am UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819 "2020-04-08T03:31:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JimLynchCodes](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jimlynchcodes/32/3090_2.png) [@JimLynchCodes](https://phaser.discourse.group/u/JimLynchCodes)\
**Post date:** [April 8, 2020, 3:31am UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/1 "2020-04-08T03:31:55Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![ReydVires](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/reydvires/32/939_2.png) [@ReydVires](https://phaser.discourse.group/u/ReydVires)\
**Post date:** [April 8, 2020, 6:21am UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/2 "2020-04-08T06:21:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JimLynchCodes](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jimlynchcodes/32/3090_2.png) [@JimLynchCodes](https://phaser.discourse.group/u/JimLynchCodes)\
**Post date:** [April 8, 2020, 7:54pm UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/3 "2020-04-08T19:54:42Z")

</div>

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… 🤔

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](https://github.com/EverybodyCodes/Phaser-Fish-Game/tree/gh-pages)

Thanks!

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 8, 2020, 8:17pm UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/4 "2020-04-08T20:17:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JimLynchCodes](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jimlynchcodes/32/3090_2.png) [@JimLynchCodes](https://phaser.discourse.group/u/JimLynchCodes)\
**Post date:** [April 8, 2020, 9:34pm UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/5 "2020-04-08T21:34:43Z")

</div>

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](https://github.com/EverybodyCodes/Phaser-Fish-Game/tree/master)

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 8, 2020, 10:08pm UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/6 "2020-04-08T22:08:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JimLynchCodes](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jimlynchcodes/32/3090_2.png) [@JimLynchCodes](https://phaser.discourse.group/u/JimLynchCodes)\
**Post date:** [April 8, 2020, 10:14pm UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/7 "2020-04-08T22:14:21Z")

</div>

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.

> <https://github.com/EverybodyCodes/Phaser-Fish-Game/blob/master/phaser-fish-single-player/src/scenes/PlayScene.ts#L151>

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 8, 2020, 10:20pm UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/8 "2020-04-08T22:20:36Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![JimLynchCodes](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jimlynchcodes/32/3090_2.png) [@JimLynchCodes](https://phaser.discourse.group/u/JimLynchCodes)\
**Post date:** [April 8, 2020, 10:27pm UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/9 "2020-04-08T22:27:12Z")

</div>

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 😢

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

```

The scene then crashes with the error:

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

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 8, 2020, 10:28pm UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/10 "2020-04-08T22:28:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JimLynchCodes](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jimlynchcodes/32/3090_2.png) [@JimLynchCodes](https://phaser.discourse.group/u/JimLynchCodes)\
**Post date:** [April 8, 2020, 10:41pm UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/11 "2020-04-08T22:41:05Z")

</div>

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. 😕

---

<div class="post-metadata">

**Author:** ![JimLynchCodes](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jimlynchcodes/32/3090_2.png) [@JimLynchCodes](https://phaser.discourse.group/u/JimLynchCodes)\
**Post date:** [April 8, 2020, 10:44pm UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/12 "2020-04-08T22:44:41Z")

</div>

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? 🤔

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:01am UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/13 "2020-04-09T00:01:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:04am UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/14 "2020-04-09T00:04:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JimLynchCodes](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jimlynchcodes/32/3090_2.png) [@JimLynchCodes](https://phaser.discourse.group/u/JimLynchCodes)\
**Post date:** [April 9, 2020, 12:16am UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/15 "2020-04-09T00:16:34Z")

</div>

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. 🤔

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:22am UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/16 "2020-04-09T00:22:16Z")

</div>

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](https://photonstorm.github.io/phaser3-docs/Phaser.Events.EventEmitter.html#removeListener)

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:32am UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/17 "2020-04-09T00:32:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![JimLynchCodes](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jimlynchcodes/32/3090_2.png) [@JimLynchCodes](https://phaser.discourse.group/u/JimLynchCodes)\
**Post date:** [April 9, 2020, 12:33am UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/18 "2020-04-09T00:33:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:34am UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/19 "2020-04-09T00:34:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![UltimateProGrammer](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/ultimateprogrammer/32/3069_2.png) [@UltimateProGrammer](https://phaser.discourse.group/u/UltimateProGrammer)\
**Post date:** [April 9, 2020, 12:39am UTC](https://phaser.discourse.group/t/how-to-restart-a-scene/5819/20 "2020-04-09T00:39:19Z")

</div>

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](http://agar.io)

[Next page](https://phaser.discourse.group/t/how-to-restart-a-scene/5819.md?page=2)
