Jump to content

game.onPause don't work when the game is paused


BunBunBun
 Share

Recommended Posts

I have the following code in the Boot.js

init: function () {   this.game.onPause.add(this.onGamePause, this);   this.game.onResume.add(this.onGameResume, this);},onGamePause: function() {   console.log('onGamePause');   if (!MainGame.supportAudio) return;   game.sound.mute = true;},onGameResume: function() {   console.log('onGameResume');   if (!MainGame.supportAudio) return;   game.sound.mute = false;},

 

then, I call "pause game" in the Game.js for example like that:

 

pauseGame: function () {    MainGame.isPaused = !MainGame.isPaused;    game.paused = MainGame.isPaused;    if(MainGame.isMusicMuted){       game.sound.setMute();    }else{       game.sound.unsetMute();    }}

 

 

and then, when the browser/tab loss focus, onGamePause and onGameResume don't work.

 

main goal: to continue to play music after calling pauseGame, it's done as you see, but if the browser/tab loss focus - mute music.

 

Link to comment
Share on other sites

But then people will ask for "don't stop animations when the game is paused" etc :)

 

I think the issue is a bit of a naming one - it's more like Phaser itself is paused, not just your game, which is why audio stops, etc.

 

You can override what happens by replacing the Game functions:

Phaser.Game.prototype.gamePaused = function (event) {        //   If the game is already paused it was done via game code, so don't re-pause it        if (!this._paused)        {            this._paused = true;            this.time.gamePaused();            //this.sound.setMute();            this.onPause.dispatch(event);        }};

So it replaces the gamePaused handler. You can do the same for gameResumed too. Then you can control what happens and why.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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