qvintusgud Posted September 20, 2018 Share Posted September 20, 2018 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 More sharing options...
samme Posted September 20, 2018 Share Posted September 20, 2018 3 hours ago, qvintusgud said: And first time I get 'restart' in the console once, next time two times, the 4, 8, 16. 32 etc. Make sure you add the event listener only once, ever. And use the latest stable Phaser. Link to comment Share on other sites More sharing options...
qvintusgud Posted September 20, 2018 Author Share Posted September 20, 2018 Yes, I just have it once. I installed Phaser with npm v 3.12.0. Can it be cache problems or anything like that? Link to comment Share on other sites More sharing options...
qvintusgud Posted September 20, 2018 Author Share Posted September 20, 2018 If just have a console log in the event listener it only logs once every time, but as soon as I add restart it double up every time Link to comment Share on other sites More sharing options...
samme Posted September 20, 2018 Share Posted September 20, 2018 Where is buttonGameRestart.addEventListener(…)? Is it inside a scene method? Link to comment Share on other sites More sharing options...
qvintusgud Posted September 21, 2018 Author Share Posted September 21, 2018 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 More sharing options...
qvintusgud Posted September 21, 2018 Author Share Posted September 21, 2018 the second time I try to restart the scene now I get a console message "Cannot read property '_pendingInsertion' of undefined" from UpdateList.js:275 Link to comment Share on other sites More sharing options...
qvintusgud Posted September 21, 2018 Author Share Posted September 21, 2018 This is my settings.. const config = { type: Phaser.AUTO, width: 420, height: 740, parent : 'gameContainer', physics: { default: 'arcade', arcade: { debug: true } }, scene: [ WelcomeScene, GameScene ] }; const game = new Phaser.Game(config); Link to comment Share on other sites More sharing options...
blackhawx Posted September 21, 2018 Share Posted September 21, 2018 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 More sharing options...
qvintusgud Posted September 22, 2018 Author Share Posted September 22, 2018 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 More sharing options...
blackhawx Posted September 23, 2018 Share Posted September 23, 2018 Webpack 4, phaser 3.12 Sounds like a different issue... Link to comment Share on other sites More sharing options...
qvintusgud Posted September 24, 2018 Author Share Posted September 24, 2018 Okay, same as me, Do you destroy the scene or something before you restart it? Link to comment Share on other sites More sharing options...
blackhawx Posted September 24, 2018 Share Posted September 24, 2018 No I do not. How are you defining and positioning buttonGameRestart in code? Link to comment Share on other sites More sharing options...
samme Posted September 24, 2018 Share Posted September 24, 2018 Move buttonGameRestart.addEventListener() into the scene constructor. fatboyarming 1 Link to comment Share on other sites More sharing options...
qvintusgud Posted September 25, 2018 Author Share Posted September 25, 2018 Great samme, that works fine! I tried to do the same from a "game over scene". The mission is to restart the previous scene. But If I try to do that it also restart and play the last scene. Is it possible to do that? Link to comment Share on other sites More sharing options...
SavedByZero Posted May 19, 2019 Share Posted May 19, 2019 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 More sharing options...
OsinPro Android Posted May 29, 2019 Share Posted May 29, 2019 var this_scene = this.scene; restart_btn.addEventListener("touchstart", function(){ this_scene.restart(); },{ passive: false }); Link to comment Share on other sites More sharing options...
Recommended Posts