I’m having an issue trying to resume game play after pausing. I have two active scenes. When a user hits ‘m’ it pauses the game scene and makes the objects in the menu scene visible. When a user click on the resume button I call this.scene.resume('Game'); to resume the game scene, but it gets stuck and keeps pausing? Here’s the code.
The approach, i.e. calling pause and resume is correct, but the way you’re doing it is causing the problem. You’re using this.keys.M.isDown in the update loop, which is going to fire a lot of times, not just once, likely queueing up a whole stack of Scene events.
It’d be better to create a Key object, or listen for the Key event, and bind a once handler to change Scene, something like this:
this.input.keyboard.once('keydown_M', function (event) {
// . Change Scene here
});
D’oh! Makes perfect sense now that you say it. One last thing though, but not that much of an issue, when I do:
this.input.keyboard.on('keydown_M', function (event) {
this.events.emit('showMenu');
}, this);
I have to hit ‘m’ twice each time I want to show the menu? Not sure why. No doubt something else I’m not doing correctly. Looks like I’m going to learn a lot about JS through Phaser
Log the flow out to the console (i.e. stick a log inside the keyboard handler, the event handler, etc) and see what’s going on. Also, you likely want to use once and not on, because you only want the event to fire once when the key is pressed, not every time.
It will only fire once, then the event will delete itself. Which is perfectly fine for changing Scene. When you return to the main Scene again, you should re-listen for the key event.