Jump to content

Pausing game, is there any drawback from doing it this way?


AzraelTycka
 Share

Recommended Posts

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

  • 2 weeks later...
 Share

  • Recently Browsing   0 members

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