mirror of
https://github.com/wassname/phaser.git
synced 2026-07-20 12:30:48 +08:00
Vastly improved visibility API support + pageshow/pagehide + focus/blur. Working across Chrome, IE, Firefox, iOS, Android (also fixes #161)
This commit is contained in:
+74
-17
@@ -86,20 +86,6 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
|
||||
*/
|
||||
this.state = null;
|
||||
|
||||
/**
|
||||
* @property {boolean} _paused - Is game paused?
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._paused = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} _loadComplete - Whether load complete loading or not.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._loadComplete = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} isBooted - Whether the game engine is booted, aka available.
|
||||
* @default
|
||||
@@ -246,6 +232,27 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
|
||||
*/
|
||||
this.stepCount = 0;
|
||||
|
||||
/**
|
||||
* @property {boolean} _paused - Is game paused?
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._paused = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} _codePaused - Was the game paused via code or a visibility change?
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._codePaused = false;
|
||||
|
||||
/**
|
||||
* @property {boolean} _loadComplete - Whether load complete loading or not.
|
||||
* @private
|
||||
* @default
|
||||
*/
|
||||
this._loadComplete = false;
|
||||
|
||||
// Parse the configuration object (if any)
|
||||
if (arguments.length === 1 && typeof arguments[0] === 'object')
|
||||
{
|
||||
@@ -605,9 +612,16 @@ Phaser.Game.prototype = {
|
||||
|
||||
if (this._paused)
|
||||
{
|
||||
this.renderer.render(this.stage);
|
||||
this.plugins.render();
|
||||
this.state.render();
|
||||
this.input.update();
|
||||
|
||||
if (this.renderType !== Phaser.HEADLESS)
|
||||
{
|
||||
this.renderer.render(this.stage);
|
||||
this.plugins.render();
|
||||
this.state.render();
|
||||
|
||||
this.plugins.postRender();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -708,6 +722,43 @@ Phaser.Game.prototype = {
|
||||
this.world = null;
|
||||
this.isBooted = false;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called by the Stage visibility handler.
|
||||
*
|
||||
* @method Phaser.Game#gamePaused
|
||||
*/
|
||||
gamePaused: function (time) {
|
||||
|
||||
// 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(time);
|
||||
this.sound.mute = true;
|
||||
this.onPause.dispatch(this);
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Called by the Stage visibility handler.
|
||||
*
|
||||
* @method Phaser.Game#gameResumed
|
||||
*/
|
||||
gameResumed: function (time) {
|
||||
|
||||
// Game is paused, but wasn't paused via code, so resume it
|
||||
if (this._paused && !this._codePaused)
|
||||
{
|
||||
this._paused = false;
|
||||
this.time.gameResumed(time);
|
||||
this.input.reset();
|
||||
this.sound.mute = false;
|
||||
this.onResume.dispatch(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
@@ -733,6 +784,9 @@ Object.defineProperty(Phaser.Game.prototype, "paused", {
|
||||
if (this._paused === false)
|
||||
{
|
||||
this._paused = true;
|
||||
this._codePaused = true;
|
||||
this.sound.mute = true;
|
||||
this.time.gamePaused();
|
||||
this.onPause.dispatch(this);
|
||||
}
|
||||
}
|
||||
@@ -741,7 +795,10 @@ Object.defineProperty(Phaser.Game.prototype, "paused", {
|
||||
if (this._paused)
|
||||
{
|
||||
this._paused = false;
|
||||
this._codePaused = false;
|
||||
this.input.reset();
|
||||
this.sound.mute = false;
|
||||
this.time.gameResumed();
|
||||
this.onResume.dispatch(this);
|
||||
}
|
||||
}
|
||||
|
||||
+42
-18
@@ -246,27 +246,37 @@ Phaser.Stage.prototype.boot = function () {
|
||||
*/
|
||||
Phaser.Stage.prototype.checkVisibility = function () {
|
||||
|
||||
var supportsVisibilityApi = false;
|
||||
var prefixes = [ "", "moz", "ms", "webkit" ];
|
||||
|
||||
while (prefixes.length)
|
||||
if (document.webkitHidden != undefined)
|
||||
{
|
||||
prefix = prefixes.pop();
|
||||
this._hiddenVar = prefix ? prefix + "Hidden" : "hidden";
|
||||
|
||||
if (this._hiddenVar in document)
|
||||
{
|
||||
supportsVisibilityApi = true;
|
||||
break;
|
||||
}
|
||||
this._hiddenVar = 'webkitvisibilitychange';
|
||||
}
|
||||
else if (document.mozHidden != undefined)
|
||||
{
|
||||
this._hiddenVar = 'mozvisibilitychange';
|
||||
}
|
||||
else if (document.msHidden != undefined)
|
||||
{
|
||||
this._hiddenVar = 'msvisibilitychange';
|
||||
}
|
||||
else if (document.hidden != undefined)
|
||||
{
|
||||
this._hiddenVar = 'visibilitychange';
|
||||
}
|
||||
else
|
||||
{
|
||||
this._hiddenVar = null;
|
||||
}
|
||||
|
||||
// Does browser support it? If not (like in IE9 or old Android) we need to fall back to blur/focus
|
||||
if (supportsVisibilityApi)
|
||||
if (this._hiddenVar)
|
||||
{
|
||||
document.addEventListener(this._hiddenVar, this._onChange, false);
|
||||
document.addEventListener('pagehide', this._onChange, false);
|
||||
document.addEventListener('pageshow', this._onChange, false);
|
||||
}
|
||||
|
||||
if (window['onpagehide'])
|
||||
{
|
||||
window.onpagehide = this._onChange;
|
||||
window.onpageshow = this._onChange;
|
||||
}
|
||||
|
||||
window.onblur = this._onChange;
|
||||
@@ -304,13 +314,27 @@ Phaser.Stage.prototype.visibilityChange = function (event) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.game.paused === false && (event.type === 'pagehide' || event.type === 'blur' || document[this._hiddenVar] === true))
|
||||
if (event.type === 'pagehide' || event.type === 'blur' || event.type === 'pageshow' || event.type === 'focus')
|
||||
{
|
||||
this.game.paused = true;
|
||||
if (event.type === 'pagehide' || event.type === 'blur')
|
||||
{
|
||||
this.game.gamePaused(event.timeStamp);
|
||||
}
|
||||
else if (event.type === 'pageshow' || event.type === 'focus')
|
||||
{
|
||||
this.game.gameResumed(event.timeStamp);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (document.hidden || document.mozHidden || document.msHidden || document.webkitHidden)
|
||||
{
|
||||
this.game.gamePaused(event.timeStamp);
|
||||
}
|
||||
else
|
||||
{
|
||||
this.game.paused = false;
|
||||
this.game.gameResumed(event.timeStamp);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user