From 86374d4437287e0c340575055739e8c25edb0e28 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Wed, 26 Feb 2014 20:12:17 +0000 Subject: [PATCH] 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) --- README.md | 4 +++- examples/wip/timer2.js | 42 ++++++++++++++++++++++++++++++++ src/time/Time.js | 5 ++-- src/time/Timer.js | 54 ++++++++++++++++++++++++++++++++++++++---- 4 files changed, 96 insertions(+), 9 deletions(-) create mode 100644 examples/wip/timer2.js diff --git a/README.md b/README.md index af16a257..23574c21 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/examples/wip/timer2.js b/examples/wip/timer2.js new file mode 100644 index 00000000..8ee7b71c --- /dev/null +++ b/examples/wip/timer2.js @@ -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() +} \ No newline at end of file diff --git a/src/time/Time.js b/src/time/Time.js index dca5e8bb..ea458711 100644 --- a/src/time/Time.js +++ b/src/time/Time.js @@ -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; diff --git a/src/time/Timer.js b/src/time/Timer.js index 96433441..7cf7e188 100644 --- a/src/time/Timer.js +++ b/src/time/Timer.js @@ -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(); } },