Skip to content

Commit 86374d4

Browse files
committed
Phaser.Timer will no longer resume if it was previously paused and the game loses focus and then resumes (fixes phaserjs#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 phaserjs#383)
1 parent db09060 commit 86374d4

4 files changed

Lines changed: 96 additions & 9 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ New features:
119119
* 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)
120120
* 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!)
121121
* 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.
122+
* 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)
122123

123124

124125
Updates:
@@ -170,7 +171,8 @@ Bug Fixes:
170171
* 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)
171172
* 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)
172173
* Fixed issue stopping SoundManager.volume from working correctly on a global volume basis (fixes #488)
173-
* The Timer will no longer create negative ticks during game boot, no matter how small the Timer delay is (fixes #366)
174+
* Phaser.Timer will no longer create negative ticks during game boot, no matter how small the Timer delay is (fixes #366)
175+
* Phaser.Timer will no longer resume if it was previously paused and the game loses focus and then resumes (fixes #383)
174176

175177

176178
TO DO:

examples/wip/timer2.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create});
2+
var counter = 0
3+
var explicitlyPaused = false
4+
5+
function create() {
6+
game.stage.backgroundColor = '#6688ee';
7+
timer = game.time.create(false);
8+
timer.repeat(Phaser.Timer.SECOND, 10, step, this);
9+
10+
game.onPause.add(handleGamePaused)
11+
game.onResume.add(handleGameResumed)
12+
13+
//prevent init tick error
14+
setTimeout(function(){
15+
timer.start()
16+
}, 250)
17+
18+
}
19+
function handleGamePaused(){
20+
console.log('game paused')
21+
}
22+
function handleGameResumed(){
23+
console.log('game resumed')
24+
}
25+
26+
function step(){
27+
counter++
28+
console.log('timer step', counter)
29+
30+
if(counter == 3){
31+
pauseTimer()
32+
}
33+
if(counter > 3){
34+
console.log('this should never happen! Paused since step #3')
35+
}
36+
}
37+
function pauseTimer(){
38+
if(explicitlyPaused) return
39+
console.log('pause timer triggered')
40+
explicitlyPaused = true
41+
timer.pause()
42+
}

src/time/Time.js

Lines changed: 2 additions & 3 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].resume();
222+
this._timers[i]._timeResume();
223223
}
224224
}
225225

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

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

305305
},
@@ -322,7 +322,6 @@ Phaser.Time.prototype = {
322322
}
323323

324324
// Level out the elapsed timer to avoid spikes
325-
326325
this.time = Date.now();
327326

328327
this._justResumed = true;

src/time/Timer.js

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ Phaser.Timer = function (game, autoDestroy) {
6666
*/
6767
this.paused = false;
6868

69+
/**
70+
* @property {boolean} _codePaused - Was the Timer paused by code or by Game focus loss?
71+
* @private
72+
*/
73+
this._codePaused = false;
74+
6975
/**
7076
* @property {number} _started - The time at which this Timer instance started running.
7177
* @private
@@ -233,11 +239,18 @@ Phaser.Timer.prototype = {
233239
/**
234240
* Stops this Timer from running. Does not cause it to be destroyed if autoDestroy is set to true.
235241
* @method Phaser.Timer#stop
242+
* @param {boolean} [clearEvents=true] - If true all the events in Timer will be cleared, otherwise they will remain.
236243
*/
237-
stop: function () {
244+
stop: function (clearEvents) {
238245

239246
this.running = false;
240-
this.events.length = 0;
247+
248+
if (typeof clearEvents === 'undefined') { clearEvents = true; }
249+
250+
if (clearEvents)
251+
{
252+
this.events.length = 0;
253+
}
241254

242255
},
243256

@@ -311,11 +324,8 @@ Phaser.Timer.prototype = {
311324
return true;
312325
}
313326

314-
// this._now = time - this._started;
315327
this._now = time;
316328

317-
// console.log('Timer update', this._now, time);
318-
319329
this._len = this.events.length;
320330

321331
this._i = 0;
@@ -404,6 +414,22 @@ Phaser.Timer.prototype = {
404414
*/
405415
pause: function () {
406416

417+
if (this.running && !this.expired)
418+
{
419+
this._pauseStarted = this.game.time.now;
420+
421+
this.paused = true;
422+
this._codePaused = true;
423+
}
424+
425+
},
426+
427+
/**
428+
* Pauses the Timer and all events in the queue.
429+
* @method Phaser.Timer#_timePause
430+
*/
431+
_timePause: function () {
432+
407433
if (this.running && !this.expired)
408434
{
409435
this._pauseStarted = this.game.time.now;
@@ -431,6 +457,24 @@ Phaser.Timer.prototype = {
431457
this.nextTick += pauseDuration;
432458

433459
this.paused = false;
460+
this._codePaused = false;
461+
}
462+
463+
},
464+
465+
/**
466+
* Resumes the Timer and updates all pending events.
467+
* @method Phaser.Timer#_timeResume
468+
*/
469+
_timeResume: function () {
470+
471+
if (this._codePaused)
472+
{
473+
return;
474+
}
475+
else
476+
{
477+
this.resume();
434478
}
435479

436480
},

0 commit comments

Comments
 (0)