Jump to content

How do I restart scene?


qvintusgud
 Share

Recommended Posts

Hi, 

 

I have made a button to restart the scene but some how it looks like it double up the scene instead of just restart. 

I use thiis

if(buttonGameRestart){
  buttonGameRestart.addEventListener('click', (e) => {
    e.preventDefault();
    console.log('restart');
    this.scene.restart();
  });
}

And first time I get 'restart' in the console once, next time two times, the 4, 8, 16. 32 etc..

Any idea how I can just reset the scene? 

First I had a game over scene and a restart button, but I get an console error after a while when the scene start over "Cannot read property 'update' of null" when I used this

const gameScene = this.scene.get('GameScene');
gameScene.scene.restart();

 

maybe someone else had a smilar problem? 

 

 

best regards

Link to comment
Share on other sites

I have it in the create function, below is some part of the code: 

class extends PhaserScene constructorsuperkey'GameScene'
    ;
  preload }

   create () {

  
 if(buttonGameRestart){
  buttonGameRestart.addEventListener('click', (e) => {
    e.preventDefault();
    console.log('restart');
    this.scene.restart();
  });
  }

}

 

Link to comment
Share on other sites

Your code looks very similar to to what I would for jQuery event handling on a web page.  But as for Phaser event handling, perhaps the following will help...

    buttonGameRestart.setInteractive();
    buttonGameRestart.on('pointerdown', (pointer, targets) => {
      this.input.addUpCallback(() => {
        this.scene.restart();
      }, true)
    });

 

This works for me in restarting my scene, within my create() method

Link to comment
Share on other sites

Hi blackhawx,

 

Thank you very much for your respond, 

I tried to use your code but I still get an error message "Uncaught TypeError: Cannot read property '_pendingInsertion' of undefined"  in UpdateList.js:275, 

I aslo had a console message within the callback and it still double up each time. 

With version do you use? Are you using webpack? 

Link to comment
Share on other sites

  • 7 months later...

Can I restart a scene and pass in parameters to its init() function at the same time?  When I do this (changing which files are loaded by setting the init parameters), the scene seems to go blank and not show anything the second time around.

[EDIT]: It turns out you can pass parameters to this.scene.restart() to the init function.  Just have to be sure to mind your closure location for "this" if you do weird stuff it with by extending loader operations beyond the preload method and passing it to helper functions.

Link to comment
Share on other sites

  • 2 weeks later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...