Pausing and Unpausing Physics in Phaser 3

I am creating a Phaser 3 game and I want to pause my game when the player presses the P key. I am currently unsure of what to do to unpause the game after the player presses the P key again. Also, I want some the text appear when they pause the game, and dissapear when they upause it. Here is my code:
`
this.key_W = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W);

this.key_A = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A);

this.key_D = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);

    this.cursorL = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.LEFT);

    this.cursorR = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.RIGHT);

    this.cursorU = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.UP);

    this.key_S = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);

    this.key_P = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.P);

`
That was all part of the create function
Now, for the update function:

`function update(){

if(this.key_W.isDown && this.player.body.touching.down){ 

            this.jump.play(); //play the jumping sound effect

	        this.player.setVelocityY(-490); //sets the jump height

}

//problem area

if(this.key_P.isDown){

      this.physics.pause();

      if(this.key_P.isDown){

               //Unpause physics here

}

}

}

`

Hi, @HackerMan ! :slight_smile: I know it’s not much, but I hope this helps. I have added a variable you can use to flip whether or not Physics is paused.

Here’s the code, I hope it works, please do let me know & correct me if I’m wrong!

this.cursorL = this.input.keyboard.addKey ( Phaser.Input.Keyboard.KeyCodes.LEFT );
this.cursorR = this.input.keyboard.addKey ( Phaser.Input.Keyboard.KeyCodes.RIGHT );
this.cursorU = this.input.keyboard.addKey ( Phaser.Input.Keyboard.KeyCodes.UP );

this.key_A = this.input.keyboard.addKey ( Phaser.Input.Keyboard.KeyCodes.A );
this.key_D = this.input.keyboard.addKey ( Phaser.Input.Keyboard.KeyCodes.D );
this.key_P = this.input.keyboard.addKey ( Phaser.Input.Keyboard.KeyCodes.P );
this.key_S = this.input.keyboard.addKey ( Phaser.Input.Keyboard.KeyCodes.S );

this.pausePhysics = false;

function update ( ) {

	if ( this.key_W.isDown && this.player.body.touching.down ) {

		// Play `Jumping` sound
		this.jump.play ( );

		// Set the `Jump` `Height`
		this.player.setVelocityY ( -490 );

	}

	// If pressing the `P` key & `physics` is `NOT` active

	if ( this.key_P.isDown && this.pausePhysics === false ) {

		// Physics becomes active
		this.pausePhysics = true;

		// Pause `Physics`
		this.physics.pause ( );

	}

	// Otherwise, if pressing the `P` key
	// & `Physics` `IS` active

	else {

		// Set `Physics` variable back to `off`
		this.pausePhysics = false;

	}

}

Once again, hope this helps & Merry Christmas! :slight_smile:

1 Like
this.physics.resume()

will reverse the pause, if that’s what you want.

1 Like

Thank you! :slight_smile:

Thank you :slight_smile: