From 8fb2f204821856373520df2761f6e26a226b7ba1 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Tue, 25 Feb 2014 03:33:47 +0000 Subject: [PATCH] Swapping between tabs will pause and resume tweens correctly, allowing their onComplete events to still fire (fixes #292) --- README.md | 1 + examples/wip/tween swap.js | 79 ++++++++++++++++++++++++++++++++++++++ src/tween/Tween.js | 13 ++++++- src/tween/TweenManager.js | 2 +- 4 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 examples/wip/tween swap.js diff --git a/README.md b/README.md index 3faeb6b79..57a71abf1 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,7 @@ Bug Fixes: * Fixed a bug where Sound.play wouldn't pick-up the local loop setting if not specified in the parameter. * Active animations now monitor if the game pauses, and resume normally when the game un-pauses (fixes #179) * Swapping between tabs will now pause the game correctly on mobile browsers (iOS7+) +* Swapping between tabs will pause and resume tweens correctly, allowing their onComplete events to still fire (fixes #292) TO DO: diff --git a/examples/wip/tween swap.js b/examples/wip/tween swap.js new file mode 100644 index 000000000..4227eb0fb --- /dev/null +++ b/examples/wip/tween swap.js @@ -0,0 +1,79 @@ + +var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render }); + +function preload() { + + game.load.spritesheet('mummy', 'assets/sprites/metalslug_mummy37x45.png', 37, 45, 18); + +} + +var mummy; +var anim; +var s = []; +var t; + +function create() { + + game.stage.backgroundColor = 0x3d4d3d; + + mummy = game.add.sprite(0, 300, 'mummy', 5); + mummy.scale.set(2); + + anim = mummy.animations.add('walk'); + + anim.play(10, true); + + 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); + +} + +function tweenOver() { + console.log('yay all over after 15 seconds anyway'); +} + +function pauseToggle() { + + if (game.paused) + { + game.paused = false; + } + else + { + game.paused = true; + } + +} + +function paused() { + + s.push('paused now: ' + game.time.now); + console.log('paused now: ' + game.time.now); + +} + +function resumed() { + + s.push('resumed now: ' + game.time.now); + console.log('resumed now: ' + game.time.now); + s.push('pause duration: ' + game.time.pauseDuration); + +} + +function update() { + +} + +function render() { + + for (var i = 0; i < s.length; i++) + { + game.debug.renderText(s[i], 16, 160 + (16 * i)); + } + +} \ No newline at end of file diff --git a/src/tween/Tween.js b/src/tween/Tween.js index 0329a7906..3a9177cb1 100644 --- a/src/tween/Tween.js +++ b/src/tween/Tween.js @@ -445,11 +445,20 @@ Phaser.Tween.prototype = { * 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 () { + resume: function (fromManager) { this._paused = false; - this._startTime += (this.game.time.now - this._pausedTime); + + if (typeof fromManager === 'undefined' || !fromManager) + { + this._startTime += (this.game.time.now - this._pausedTime); + } + else + { + this._startTime += this.game.time.pauseDuration; + } }, diff --git a/src/tween/TweenManager.js b/src/tween/TweenManager.js index 7fa0f202c..c9af98436 100644 --- a/src/tween/TweenManager.js +++ b/src/tween/TweenManager.js @@ -194,7 +194,7 @@ Phaser.TweenManager.prototype = { for (var i = this._tweens.length - 1; i >= 0; i--) { - this._tweens[i].resume(); + this._tweens[i].resume(true); } }