Skip to content

Commit 4d28402

Browse files
committed
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.
1 parent 86374d4 commit 4d28402

6 files changed

Lines changed: 114 additions & 20 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ Bug Fixes:
173173
* Fixed issue stopping SoundManager.volume from working correctly on a global volume basis (fixes #488)
174174
* Phaser.Timer will no longer create negative ticks during game boot, no matter how small the Timer delay is (fixes #366)
175175
* Phaser.Timer will no longer resume if it was previously paused and the game loses focus and then resumes (fixes #383)
176+
* Tweens now resume correctly if the game pauses (focus loss) while they are paused.
177+
* Tweens don't double pause if they were already paused and the game pauses.
178+
176179

177180

178181
TO DO:

examples/wip/tween swap.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,36 @@ function create() {
2323

2424
anim.play(10, true);
2525

26-
game.onPause.add(paused, this);
27-
game.onResume.add(resumed, this);
26+
// game.onPause.add(paused, this);
27+
// game.onResume.add(resumed, this);
2828

2929
t = game.add.tween(mummy).to({x:700}, 15000, Phaser.Easing.Linear.None, true);
3030
t.onComplete.add(tweenOver, this);
3131

3232
s.push('starting: ' + game.stage._hiddenVar);
3333

34+
game.input.onDown.add(pauseTween, this);
35+
36+
}
37+
38+
function pauseTween(pointer) {
39+
40+
if (pointer.x < 400)
41+
{
42+
t.pause();
43+
}
44+
else
45+
{
46+
t.resume();
47+
}
48+
3449
}
3550

3651
function tweenOver() {
3752
console.log('yay all over after 15 seconds anyway');
3853
}
3954

40-
function pauseToggle() {
55+
/*function pauseToggle() {
4156
4257
if (game.paused)
4358
{
@@ -49,7 +64,6 @@ function pauseToggle() {
4964
}
5065
5166
}
52-
5367
function paused() {
5468
5569
s.push('paused now: ' + game.time.now);
@@ -64,6 +78,7 @@ function resumed() {
6478
s.push('pause duration: ' + game.time.pauseDuration);
6579
6680
}
81+
*/
6782

6883
function update() {
6984

src/time/Time.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ Phaser.Time.prototype = {
219219

220220
for (var i = 0; i < this._timers.length; i++)
221221
{
222-
this._timers[i]._timeResume();
222+
this._timers[i]._resume();
223223
}
224224
}
225225

@@ -299,7 +299,7 @@ Phaser.Time.prototype = {
299299

300300
while (i--)
301301
{
302-
this._timers[i]._timePause();
302+
this._timers[i]._pause();
303303
}
304304

305305
},

src/time/Timer.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,11 @@ Phaser.Timer.prototype = {
425425
},
426426

427427
/**
428-
* Pauses the Timer and all events in the queue.
429-
* @method Phaser.Timer#_timePause
428+
* This is called by the core Game loop. Do not call it directly, instead use Timer.pause.
429+
* @method Phaser.Timer#_pause
430+
* @private
430431
*/
431-
_timePause: function () {
432+
_pause: function () {
432433

433434
if (this.running && !this.expired)
434435
{
@@ -463,10 +464,11 @@ Phaser.Timer.prototype = {
463464
},
464465

465466
/**
466-
* Resumes the Timer and updates all pending events.
467-
* @method Phaser.Timer#_timeResume
467+
* This is called by the core Game loop. Do not call it directly, instead use Timer.pause.
468+
* @method Phaser.Timer#_resume
469+
* @private
468470
*/
469-
_timeResume: function () {
471+
_resume: function () {
470472

471473
if (this._codePaused)
472474
{

src/tween/Tween.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,26 @@ Phaser.Tween = function (object, game) {
132132
*/
133133
this._onUpdateCallbackContext = null;
134134

135+
/**
136+
* @property {boolean} _paused - Is this Tween paused or not?
137+
* @private
138+
* @default
139+
*/
140+
this._paused = false;
141+
135142
/**
136143
* @property {number} _pausedTime - Private pause timer.
137144
* @private
138145
* @default
139146
*/
140147
this._pausedTime = 0;
141148

149+
/**
150+
* @property {boolean} _codePaused - Was the Tween paused by code or by Game focus loss?
151+
* @private
152+
*/
153+
this._codePaused = false;
154+
142155
/**
143156
* @property {boolean} pendingDelete - If this tween is ready to be deleted by the TweenManager.
144157
* @default
@@ -436,28 +449,59 @@ Phaser.Tween.prototype = {
436449
*/
437450
pause: function () {
438451

452+
this._codePaused = true;
439453
this._paused = true;
440454
this._pausedTime = this.game.time.now;
441455

442456
},
443457

458+
/**
459+
* This is called by the core Game loop. Do not call it directly, instead use Tween.pause.
460+
* @method Phaser.Tween#_pause
461+
* @private
462+
*/
463+
_pause: function () {
464+
465+
if (!this._codePaused)
466+
{
467+
this._paused = true;
468+
this._pausedTime = this.game.time.now;
469+
}
470+
471+
},
472+
444473
/**
445474
* Resumes a paused tween.
446475
*
447476
* @method Phaser.Tween#resume
448-
* @param {boolean} [fromManager=false] - Did this resume request come from the TweenManager or game code?
449477
*/
450-
resume: function (fromManager) {
478+
resume: function () {
451479

452-
this._paused = false;
453-
454-
if (typeof fromManager === 'undefined' || !fromManager)
480+
if (this._paused)
455481
{
482+
this._paused = false;
483+
this._codePaused = false;
484+
456485
this._startTime += (this.game.time.now - this._pausedTime);
457486
}
487+
488+
},
489+
490+
/**
491+
* This is called by the core Game loop. Do not call it directly, instead use Tween.pause.
492+
* @method Phaser.Tween#_resume
493+
* @private
494+
*/
495+
_resume: function () {
496+
497+
if (this._codePaused)
498+
{
499+
return;
500+
}
458501
else
459502
{
460503
this._startTime += this.game.time.pauseDuration;
504+
this._paused = false;
461505
}
462506

463507
},

src/tween/TweenManager.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ Phaser.TweenManager = function (game) {
3939
*/
4040
this._add = [];
4141

42-
this.game.onPause.add(this.pauseAll, this);
43-
this.game.onResume.add(this.resumeAll, this);
42+
this.game.onPause.add(this._pauseAll, this);
43+
this.game.onResume.add(this._resumeAll, this);
4444

4545
};
4646

@@ -171,6 +171,36 @@ Phaser.TweenManager.prototype = {
171171

172172
},
173173

174+
/**
175+
* Private. Called by game focus loss. Pauses all currently running tweens.
176+
*
177+
* @method Phaser.TweenManager#_pauseAll
178+
* @private
179+
*/
180+
_pauseAll: function () {
181+
182+
for (var i = this._tweens.length - 1; i >= 0; i--)
183+
{
184+
this._tweens[i]._pause();
185+
}
186+
187+
},
188+
189+
/**
190+
* Private. Called by game focus loss. Resumes all currently paused tweens.
191+
*
192+
* @method Phaser.TweenManager#_resumeAll
193+
* @private
194+
*/
195+
_resumeAll: function () {
196+
197+
for (var i = this._tweens.length - 1; i >= 0; i--)
198+
{
199+
this._tweens[i]._resume();
200+
}
201+
202+
},
203+
174204
/**
175205
* Pauses all currently running tweens.
176206
*
@@ -186,7 +216,7 @@ Phaser.TweenManager.prototype = {
186216
},
187217

188218
/**
189-
* Pauses all currently paused tweens.
219+
* Resumes all currently paused tweens.
190220
*
191221
* @method Phaser.TweenManager#resumeAll
192222
*/

0 commit comments

Comments
 (0)