Phaser.Timer will no longer resume if it was previously paused and the game loses focus and then resumes (fixes #383)

Phaser.Timer.stop has a new parameter: clearEvents (default true), if true all the events in Timer will be cleared, otherwise they will remain (fixes #383)
This commit is contained in:
photonstorm 2014-02-26 20:12:17 +00:00
parent db090601b8
commit 86374d4437
4 changed files with 96 additions and 9 deletions

View file

@ -119,6 +119,7 @@ New features:
* Sprite.smoothed and Image.smoothed allows you to set per-Sprite smoothing, perfect if you just want to keep a few sprites smoothed (or not)
* StateManager.start can now have as many parameters as you like. The order is: start(key, clearWorld, clearCache, ...) - they are passed to State.init() (NOT create!)
* Loader.script now has callback (and callbackContext) parameters, so you can specify a function to run once the JS has been injected into the body.
* Phaser.Timer.stop has a new parameter: clearEvents (default true), if true all the events in Timer will be cleared, otherwise they will remain (fixes #383)
Updates:
@ -170,7 +171,8 @@ Bug Fixes:
* AnimationParser.spriteSheet wasn't taking the margin or spacing into account when calculating the numbers of sprites per row/column, nor was it allowing for extra power-of-two padding at the end (fix #482, thanks yig)
* AnimationManager.add documentation said that 'frames' could be null, but the code couldn't handle this so it defaults to an empty array if none given (thanks yig)
* Fixed issue stopping SoundManager.volume from working correctly on a global volume basis (fixes #488)
* The Timer will no longer create negative ticks during game boot, no matter how small the Timer delay is (fixes #366)
* Phaser.Timer will no longer create negative ticks during game boot, no matter how small the Timer delay is (fixes #366)
* Phaser.Timer will no longer resume if it was previously paused and the game loses focus and then resumes (fixes #383)
TO DO:

42
examples/wip/timer2.js Normal file
View file

@ -0,0 +1,42 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create});
var counter = 0
var explicitlyPaused = false
function create() {
game.stage.backgroundColor = '#6688ee';
timer = game.time.create(false);
timer.repeat(Phaser.Timer.SECOND, 10, step, this);
game.onPause.add(handleGamePaused)
game.onResume.add(handleGameResumed)
//prevent init tick error
setTimeout(function(){
timer.start()
}, 250)
}
function handleGamePaused(){
console.log('game paused')
}
function handleGameResumed(){
console.log('game resumed')
}
function step(){
counter++
console.log('timer step', counter)
if(counter == 3){
pauseTimer()
}
if(counter > 3){
console.log('this should never happen! Paused since step #3')
}
}
function pauseTimer(){
if(explicitlyPaused) return
console.log('pause timer triggered')
explicitlyPaused = true
timer.pause()
}

View file

@ -219,7 +219,7 @@ Phaser.Time.prototype = {
for (var i = 0; i < this._timers.length; i++)
{
this._timers[i].resume();
this._timers[i]._timeResume();
}
}
@ -299,7 +299,7 @@ Phaser.Time.prototype = {
while (i--)
{
this._timers[i].pause();
this._timers[i]._timePause();
}
},
@ -322,7 +322,6 @@ Phaser.Time.prototype = {
}
// Level out the elapsed timer to avoid spikes
this.time = Date.now();
this._justResumed = true;

View file

@ -66,6 +66,12 @@ Phaser.Timer = function (game, autoDestroy) {
*/
this.paused = false;
/**
* @property {boolean} _codePaused - Was the Timer paused by code or by Game focus loss?
* @private
*/
this._codePaused = false;
/**
* @property {number} _started - The time at which this Timer instance started running.
* @private
@ -233,11 +239,18 @@ Phaser.Timer.prototype = {
/**
* Stops this Timer from running. Does not cause it to be destroyed if autoDestroy is set to true.
* @method Phaser.Timer#stop
* @param {boolean} [clearEvents=true] - If true all the events in Timer will be cleared, otherwise they will remain.
*/
stop: function () {
stop: function (clearEvents) {
this.running = false;
this.events.length = 0;
if (typeof clearEvents === 'undefined') { clearEvents = true; }
if (clearEvents)
{
this.events.length = 0;
}
},
@ -311,11 +324,8 @@ Phaser.Timer.prototype = {
return true;
}
// this._now = time - this._started;
this._now = time;
// console.log('Timer update', this._now, time);
this._len = this.events.length;
this._i = 0;
@ -404,6 +414,22 @@ Phaser.Timer.prototype = {
*/
pause: function () {
if (this.running && !this.expired)
{
this._pauseStarted = this.game.time.now;
this.paused = true;
this._codePaused = true;
}
},
/**
* Pauses the Timer and all events in the queue.
* @method Phaser.Timer#_timePause
*/
_timePause: function () {
if (this.running && !this.expired)
{
this._pauseStarted = this.game.time.now;
@ -431,6 +457,24 @@ Phaser.Timer.prototype = {
this.nextTick += pauseDuration;
this.paused = false;
this._codePaused = false;
}
},
/**
* Resumes the Timer and updates all pending events.
* @method Phaser.Timer#_timeResume
*/
_timeResume: function () {
if (this._codePaused)
{
return;
}
else
{
this.resume();
}
},