Advertisement
Guest User

Phaser custom pause system

a guest
Mar 20th, 2014
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Pausing in Phaser
  2.  
  3. Create a variable like so:
  4.  
  5. var gamepaused;
  6.  
  7. then in create have it as:
  8.  
  9. gamepaused = 0; // this is the unpaused state
  10.  
  11. next set up either a keystroke or an onscreen button,
  12. whichever you prefer.
  13.  
  14. Now set up the pause function like i do here:
  15.  
  16. pauseGame: function(pointer)
  17.             {
  18.                 PausedGame = 1;
  19.  
  20.                 gamePaused.destroy();
  21.  
  22.                 pausetext = this.add.text(220, 180,'Game Paused',{
  23.                 font: "52px Courier",
  24.                 fill: "#1e85f1",
  25.                 stroke: "#000000",
  26.                 strokeThickness: 2
  27.                 });
  28.                 pausetext.fixedToCamera = true;
  29.  
  30.                 pausePlatform = this.add.sprite(player.x - 5, player.y + 13, 'ledge');
  31.                 pausePlatform.body.immovable = true;
  32.                 pausePlatform.collideWorldBounds = true;
  33.                 pausePlatform.allowGravity = false;
  34.                 pausePlatform.renderable = false;
  35.  
  36.                 unPause = this.add.button(this.game.width - 100, 10, 'pausebutn', this.unPauseGame, this, 0);
  37.                 unPause.fixedToCamera = true;
  38.                 unPause.alpha = 0.4;
  39.             }
  40.  
  41. Now i created a platform and set its render to false to that my player didnt slow descend
  42. a pixel at a time. The code is int he pause function above for a look at it.
  43.  
  44. Next i have an unpause function like so:
  45.  
  46. unPauseGame: function(pointer)
  47.             {
  48.                 PausedGame = 0
  49.  
  50.                 unPause.destroy();
  51.  
  52.                 pausetext.destroy();
  53.  
  54.                 pausePlatform.destroy();
  55.  
  56.                 this.input.keyboard.disabled = false;
  57.                 this.input.pointer1.disabled = false;
  58.  
  59.                 gamePaused = this.add.button(this.game.width - 100, 10, 'pausebutn', this.pauseGame, this, 0);
  60.                 gamePaused.fixedToCamera = true;
  61.                 gamePaused.alpha = 0.4;
  62.  
  63.                 pausetext.destroy();
  64.                
  65.             }
  66.  
  67. The button in my game is replaced with a duplicate button and switched and destroyed
  68. without the player ever knowing or seeing any difference. This is also done in a memory
  69. safe and controlled way.
  70.  
  71. Then in my update loop i have a little snip to stop a few physics and player specific
  72. things like so:
  73.  
  74. if(PausedGame === 1)
  75.             {
  76.                 this.input.keyboard.disabled = true;
  77.                 this.input.pointer1.disabled = true;
  78.  
  79.                 player.body.velocity.y = 0;
  80.                 player.body.velocity.x = 0;
  81.                 player.body.allowGravity = false;
  82.  
  83.                 pausetext.exists = true;
  84.             }
  85.  
  86.  
  87. @author: @Heppell08
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement