Skip to content

Commit 68d5c73

Browse files
committed
Explicitly paused Timer continues if you un-focus and focus the browser window.
Added TimerEvent.pendingDelete and checks in Timer.update, so that removing an event in a callback no longer throws an exception.
1 parent eddce65 commit 68d5c73

4 files changed

Lines changed: 64 additions & 26 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ Updates:
7373

7474
Bug Fixes:
7575

76+
* Explicitly paused Timer continues if you un-focus and focus the browser window (thanks georgiee)
77+
* Added TimerEvent.pendingDelete and checks in Timer.update, so that removing an event in a callback no longer throws an exception (thanks georgiee)
7678

7779

7880
You can view the Change Log for all previous versions at https://github.com/photonstorm/phaser/changelog.md

examples/wip/timer simple.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,53 @@
11

2-
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, update: update, render: render });
2+
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create, render: render });
33

44
function preload() {
55

6-
game.load.image('mushroom', 'assets/sprites/mushroom2.png');
7-
game.load.image('sonic', 'assets/sprites/pangball.png');
6+
game.load.image('ball', 'assets/sprites/pangball.png');
87

98
}
109

11-
var timer;
10+
var t;
1211

1312
function create() {
1413

15-
game.stage.backgroundColor = '#007236';
14+
game.stage.backgroundColor = '#6688ee';
1615

16+
t = game.time.create(false);
1717

18+
t.repeat(Phaser.Timer.SECOND * 2, 10, createBall, this);
19+
t.repeat(Phaser.Timer.SECOND * 3, 10, createBall, this);
1820

19-
// Every second we will call the addSprite function. This will happen 10 times and then stop.
20-
// The final parameter is the one that will be sent to the addSprite function and in this case is the sprite key.
21-
game.time.repeatEvent(Phaser.Timer.SECOND, 10, addSprite, this, 'mushroom');
21+
t.start();
2222

23-
// Every 1.5 seconds we will call the addSprite function. This will happen 5 times and then stop.
24-
game.time.repeatEvent(1500, 10, addSprite, this, 'sonic');
23+
game.input.onDown.add(killThem, this);
2524

2625
}
2726

28-
function addSprite(key) {
27+
function createBall() {
2928

30-
console.log(arguments);
29+
// A bouncey ball sprite just to visually see what's going on.
30+
var ball = game.add.sprite(game.world.randomX, 0, 'ball');
31+
32+
ball.body.gravity.y = 200;
33+
ball.body.bounce.y = 0.5;
34+
ball.body.collideWorldBounds = true;
35+
36+
console.log('nuked');
37+
game.time.removeAll();
38+
// t.removeAll();
3139

32-
game.add.sprite(game.world.randomX, game.world.randomY, key);
3340

3441
}
3542

36-
function update() {
43+
function killThem() {
44+
3745

3846
}
3947

4048
function render() {
4149

42-
game.debug.renderText(game.time._timer.ms, 32, 32);
43-
// game.debug.renderCameraInfo(game.camera, 32, 32);
50+
game.debug.renderText("Time until event: " + t.duration.toFixed(0), 32, 32);
51+
game.debug.renderText("Next tick: " + t.next.toFixed(0), 32, 64);
4452

4553
}

src/time/Timer.js

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Phaser.Timer.prototype = {
245245
{
246246
if (this.events[i] === event)
247247
{
248-
this.events.splice(i, 1);
248+
this.events[i].pendingDelete = true;
249249
return true;
250250
}
251251
}
@@ -308,6 +308,21 @@ Phaser.Timer.prototype = {
308308

309309
this._len = this.events.length;
310310

311+
this._i = 0;
312+
313+
while (this._i < this._len)
314+
{
315+
if (this.events[this._i].pendingDelete)
316+
{
317+
this.events.splice(this._i, 1);
318+
this._len--;
319+
}
320+
321+
this._i++;
322+
}
323+
324+
this._len = this.events.length;
325+
311326
if (this.running && this._now >= this.nextTick && this._len > 0)
312327
{
313328
this._i = 0;
@@ -371,9 +386,12 @@ Phaser.Timer.prototype = {
371386
*/
372387
pause: function () {
373388

374-
this._pauseStarted = this.game.time.now;
389+
if (this.running && !this.expired)
390+
{
391+
this._pauseStarted = this.game.time.now;
375392

376-
this.paused = true;
393+
this.paused = true;
394+
}
377395

378396
},
379397

@@ -383,16 +401,19 @@ Phaser.Timer.prototype = {
383401
*/
384402
resume: function () {
385403

386-
var pauseDuration = this.game.time.now - this._pauseStarted;
387-
388-
for (var i = 0; i < this.events.length; i++)
404+
if (this.running && !this.expired)
389405
{
390-
this.events[i].tick += pauseDuration;
391-
}
406+
var pauseDuration = this.game.time.now - this._pauseStarted;
392407

393-
this.nextTick += pauseDuration;
408+
for (var i = 0; i < this.events.length; i++)
409+
{
410+
this.events[i].tick += pauseDuration;
411+
}
394412

395-
this.paused = false;
413+
this.nextTick += pauseDuration;
414+
415+
this.paused = false;
416+
}
396417

397418
},
398419

@@ -405,6 +426,7 @@ Phaser.Timer.prototype = {
405426
this.onComplete.removeAll();
406427
this.running = false;
407428
this.events = [];
429+
this._i = this._len;
408430

409431
}
410432

src/time/TimerEvent.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ Phaser.TimerEvent = function (timer, delay, tick, repeatCount, loop, callback, c
6262
*/
6363
this.args = args;
6464

65+
/**
66+
* @property {boolean} pendingDelete - A flag that controls if the TimerEvent is pending deletion.
67+
* @protected
68+
*/
69+
this.pendingDelete = false;
70+
6571
};
6672

6773
Phaser.TimerEvent.prototype.constructor = Phaser.TimerEvent;

0 commit comments

Comments
 (0)