JavaNerd Posted May 22, 2018 Share Posted May 22, 2018 In my game, when I change scenes from the main scene, to another, and then back to the main scene, it starts picking up additional input actions. For example, if I put console.log("") in: this.input.on('pointerup', function () {}, this); it will print 4 times. Any idea on how to fix this or what is going on? Link to comment Share on other sites More sharing options...
samme Posted May 22, 2018 Share Posted May 22, 2018 If you restart or stop/start a scene, its init → preload → create calls are repeated, so if you're adding event listeners there they'll be repeated. Link to comment Share on other sites More sharing options...
JavaNerd Posted May 22, 2018 Author Share Posted May 22, 2018 Okay, so this is weird. I left my input listeners in my create function, but surrounded it with this if(gameLoaded) { } at the bottom of my create function, I put: gameLoaded = true; This prevented my input listeners from being repeated. But then my console.log went from 4 to 2 times per click. Then after each time of leaving the scene and coming back, it would go up by one. I then removed the gameLoaded check from around my input listeners and then added this code: if(gameLoaded % 2) { this.scene.restart(); } and put it at the top of my create function. and put this at the bottom of my create function: gameLoaded++; with gameLoaded set to 1 initially. Now my game works as intended, but this seems like a bit of a hacky workaround. Is there a better way to accomplish not repeating input listeners and changing scenes back and forth? Link to comment Share on other sites More sharing options...
samme Posted May 23, 2018 Share Posted May 23, 2018 Are you using the latest Phaser release? Link to comment Share on other sites More sharing options...
JavaNerd Posted May 23, 2018 Author Share Posted May 23, 2018 2 hours ago, samme said: Are you using the latest Phaser release? Yes Link to comment Share on other sites More sharing options...
samme Posted May 23, 2018 Share Posted May 23, 2018 17 hours ago, JavaNerd said: But then my console.log went from 4 to 2 times per click. Then after each time of leaving the scene and coming back, it would go up by one. Can you add one console.log at the start of your init or create function? I wonder if your scene is getting started twice somehow. Link to comment Share on other sites More sharing options...
JavaNerd Posted May 28, 2018 Author Share Posted May 28, 2018 On 5/23/2018 at 12:00 PM, samme said: Can you add one console.log at the start of your init or create function? I wonder if your scene is getting started twice somehow. It does not get started twice. Link to comment Share on other sites More sharing options...
Recommended Posts