AzraelTycka Posted April 19, 2016 Share Posted April 19, 2016 Hello, I'm finishing my game with Phaser and I got into implementing a pause and restarting the game when it's game over. I'd like to ask you more experienced phaser developers if the way I currently implementted is fine or if there are any drawbacks? I'm not sure if I'm not touching something I shouldn't have, so far the pause and restart do the thing perfectly but well inviting an unseen bug this way coul be also possible so I'd like to make sure. First of all I should have said that I wanted to implement pause, unpause and restart purely with phaser functionality without going to eventlistener outside the game (well it's the same thing but I expect that phaser has some shims and is ready for unexpected cases so I don't need to take care of them first). So I pause/unpause game with this.game.paused = true/false, when the game is over I do this.game.paused = true; after displaying a small window with score and text "Press space to continue". So I keep vision of the game stage in background when the game is over (I don't move to other state) and only if the space bar is pressed I restart main game state. Because I used game.paused which disables update function and all other things I went with this.game.input.keyboard.onDownCallback which has a check for game.paused && game.gameover to make sure that althought it is called on every key press it doesn't progress to do anything until the game is really over. So I'd like to ask if this part with keyboard.onDownCallback is fine, I set it manually but I'm not sure if it's not some generic thing which phaser uses for something else? Code: var style = {font: '28px Times New Roman', fill: '#ffffff'}; this.pauseText = this.game.add.text(this.game.width - 10, Math.floor(this.topBarHeight / 2), 'PAUSE', style); this.pauseText.inputEnabled = true; this.pauseText.events.onInputDown.add(this.pause, this); this.game.input.onDown.add(this.onKeyDown, this); this.game.input.keyboard.onDownCallback = this.onKeyDown.bind(this); pause: function() { if (!this.game.paused) { this.pauseScreen.renderable = true; this.game.paused = true; } }, unpause: function() { if (this.game.paused) { this.pauseScreen.renderable = false; this.game.paused = false; } }, onKeyDown: function() { if (this.game.paused && !this.gameover) { this.unpause(); } else if (this.game.paused && this.gameover) { if (this.spaceBar.isDown) { this.game.paused = false; this.state.start('game'); } } }, gameOver: function() { this.gameover = true; this.updateGameOverScreen(); this.gameOverBMD.addToWorld(Math.floor(this.game.width / 2 - this.goW / 2), Math.floor(this.game.height / 2 - this.goH / 2)); this.game.paused = true; return true; } Thank you, I think it's not a problem but I'd like to make sure with you. Link to comment Share on other sites More sharing options...
drhayes Posted April 19, 2016 Share Posted April 19, 2016 That's pretty much what I do in my game. AzraelTycka 1 Link to comment Share on other sites More sharing options...
AzraelTycka Posted April 19, 2016 Author Share Posted April 19, 2016 Thank you. Link to comment Share on other sites More sharing options...
isekream Posted April 29, 2016 Share Posted April 29, 2016 Hey, Why would the code in this thread not work? Link to comment Share on other sites More sharing options...
Recommended Posts