Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Pausing in Phaser
- Create a variable like so:
- var gamepaused;
- then in create have it as:
- gamepaused = 0; // this is the unpaused state
- next set up either a keystroke or an onscreen button,
- whichever you prefer.
- Now set up the pause function like i do here:
- pauseGame: function(pointer)
- {
- PausedGame = 1;
- gamePaused.destroy();
- pausetext = this.add.text(220, 180,'Game Paused',{
- font: "52px Courier",
- fill: "#1e85f1",
- stroke: "#000000",
- strokeThickness: 2
- });
- pausetext.fixedToCamera = true;
- pausePlatform = this.add.sprite(player.x - 5, player.y + 13, 'ledge');
- pausePlatform.body.immovable = true;
- pausePlatform.collideWorldBounds = true;
- pausePlatform.allowGravity = false;
- pausePlatform.renderable = false;
- unPause = this.add.button(this.game.width - 100, 10, 'pausebutn', this.unPauseGame, this, 0);
- unPause.fixedToCamera = true;
- unPause.alpha = 0.4;
- }
- Now i created a platform and set its render to false to that my player didnt slow descend
- a pixel at a time. The code is int he pause function above for a look at it.
- Next i have an unpause function like so:
- unPauseGame: function(pointer)
- {
- PausedGame = 0
- unPause.destroy();
- pausetext.destroy();
- pausePlatform.destroy();
- this.input.keyboard.disabled = false;
- this.input.pointer1.disabled = false;
- gamePaused = this.add.button(this.game.width - 100, 10, 'pausebutn', this.pauseGame, this, 0);
- gamePaused.fixedToCamera = true;
- gamePaused.alpha = 0.4;
- pausetext.destroy();
- }
- The button in my game is replaced with a duplicate button and switched and destroyed
- without the player ever knowing or seeing any difference. This is also done in a memory
- safe and controlled way.
- Then in my update loop i have a little snip to stop a few physics and player specific
- things like so:
- if(PausedGame === 1)
- {
- this.input.keyboard.disabled = true;
- this.input.pointer1.disabled = true;
- player.body.velocity.y = 0;
- player.body.velocity.x = 0;
- player.body.allowGravity = false;
- pausetext.exists = true;
- }
- @author: @Heppell08
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement