diff --git a/README.md b/README.md index 23574c21..8f8fb3f6 100644 --- a/README.md +++ b/README.md @@ -173,6 +173,9 @@ Bug Fixes: * Fixed issue stopping SoundManager.volume from working correctly on a global volume basis (fixes #488) * 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) +* Tweens now resume correctly if the game pauses (focus loss) while they are paused. +* Tweens don't double pause if they were already paused and the game pauses. + TO DO: diff --git a/examples/wip/tween swap.js b/examples/wip/tween swap.js index 4227eb0f..6bf66563 100644 --- a/examples/wip/tween swap.js +++ b/examples/wip/tween swap.js @@ -23,21 +23,36 @@ function create() { anim.play(10, true); - game.onPause.add(paused, this); - game.onResume.add(resumed, this); + // game.onPause.add(paused, this); + // game.onResume.add(resumed, this); t = game.add.tween(mummy).to({x:700}, 15000, Phaser.Easing.Linear.None, true); t.onComplete.add(tweenOver, this); s.push('starting: ' + game.stage._hiddenVar); + game.input.onDown.add(pauseTween, this); + +} + +function pauseTween(pointer) { + + if (pointer.x < 400) + { + t.pause(); + } + else + { + t.resume(); + } + } function tweenOver() { console.log('yay all over after 15 seconds anyway'); } -function pauseToggle() { +/*function pauseToggle() { if (game.paused) { @@ -49,7 +64,6 @@ function pauseToggle() { } } - function paused() { s.push('paused now: ' + game.time.now); @@ -64,6 +78,7 @@ function resumed() { s.push('pause duration: ' + game.time.pauseDuration); } +*/ function update() { diff --git a/src/time/Time.js b/src/time/Time.js index ea458711..dbe72023 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]._timeResume(); + this._timers[i]._resume(); } } @@ -299,7 +299,7 @@ Phaser.Time.prototype = { while (i--) { - this._timers[i]._timePause(); + this._timers[i]._pause(); } }, diff --git a/src/time/Timer.js b/src/time/Timer.js index 7cf7e188..f5755aea 100644 --- a/src/time/Timer.js +++ b/src/time/Timer.js @@ -425,10 +425,11 @@ Phaser.Timer.prototype = { }, /** - * Pauses the Timer and all events in the queue. - * @method Phaser.Timer#_timePause + * This is called by the core Game loop. Do not call it directly, instead use Timer.pause. + * @method Phaser.Timer#_pause + * @private */ - _timePause: function () { + _pause: function () { if (this.running && !this.expired) { @@ -463,10 +464,11 @@ Phaser.Timer.prototype = { }, /** - * Resumes the Timer and updates all pending events. - * @method Phaser.Timer#_timeResume + * This is called by the core Game loop. Do not call it directly, instead use Timer.pause. + * @method Phaser.Timer#_resume + * @private */ - _timeResume: function () { + _resume: function () { if (this._codePaused) { diff --git a/src/tween/Tween.js b/src/tween/Tween.js index 3a9177cb..f21cd643 100644 --- a/src/tween/Tween.js +++ b/src/tween/Tween.js @@ -132,6 +132,13 @@ Phaser.Tween = function (object, game) { */ this._onUpdateCallbackContext = null; + /** + * @property {boolean} _paused - Is this Tween paused or not? + * @private + * @default + */ + this._paused = false; + /** * @property {number} _pausedTime - Private pause timer. * @private @@ -139,6 +146,12 @@ Phaser.Tween = function (object, game) { */ this._pausedTime = 0; + /** + * @property {boolean} _codePaused - Was the Tween paused by code or by Game focus loss? + * @private + */ + this._codePaused = false; + /** * @property {boolean} pendingDelete - If this tween is ready to be deleted by the TweenManager. * @default @@ -436,28 +449,59 @@ Phaser.Tween.prototype = { */ pause: function () { + this._codePaused = true; this._paused = true; this._pausedTime = this.game.time.now; }, + /** + * This is called by the core Game loop. Do not call it directly, instead use Tween.pause. + * @method Phaser.Tween#_pause + * @private + */ + _pause: function () { + + if (!this._codePaused) + { + this._paused = true; + this._pausedTime = this.game.time.now; + } + + }, + /** * Resumes a paused tween. * * @method Phaser.Tween#resume - * @param {boolean} [fromManager=false] - Did this resume request come from the TweenManager or game code? */ - resume: function (fromManager) { + resume: function () { - this._paused = false; - - if (typeof fromManager === 'undefined' || !fromManager) + if (this._paused) { + this._paused = false; + this._codePaused = false; + this._startTime += (this.game.time.now - this._pausedTime); } + + }, + + /** + * This is called by the core Game loop. Do not call it directly, instead use Tween.pause. + * @method Phaser.Tween#_resume + * @private + */ + _resume: function () { + + if (this._codePaused) + { + return; + } else { this._startTime += this.game.time.pauseDuration; + this._paused = false; } }, diff --git a/src/tween/TweenManager.js b/src/tween/TweenManager.js index c9af9843..ed567fa7 100644 --- a/src/tween/TweenManager.js +++ b/src/tween/TweenManager.js @@ -39,8 +39,8 @@ Phaser.TweenManager = function (game) { */ this._add = []; - this.game.onPause.add(this.pauseAll, this); - this.game.onResume.add(this.resumeAll, this); + this.game.onPause.add(this._pauseAll, this); + this.game.onResume.add(this._resumeAll, this); }; @@ -171,6 +171,36 @@ Phaser.TweenManager.prototype = { }, + /** + * Private. Called by game focus loss. Pauses all currently running tweens. + * + * @method Phaser.TweenManager#_pauseAll + * @private + */ + _pauseAll: function () { + + for (var i = this._tweens.length - 1; i >= 0; i--) + { + this._tweens[i]._pause(); + } + + }, + + /** + * Private. Called by game focus loss. Resumes all currently paused tweens. + * + * @method Phaser.TweenManager#_resumeAll + * @private + */ + _resumeAll: function () { + + for (var i = this._tweens.length - 1; i >= 0; i--) + { + this._tweens[i]._resume(); + } + + }, + /** * Pauses all currently running tweens. * @@ -186,7 +216,7 @@ Phaser.TweenManager.prototype = { }, /** - * Pauses all currently paused tweens. + * Resumes all currently paused tweens. * * @method Phaser.TweenManager#resumeAll */