Skip to content

Commit

Permalink
Explicitly paused Timer continues if you un-focus and focus the brows…
Browse files Browse the repository at this point in the history
…er window.

Added TimerEvent.pendingDelete and checks in Timer.update, so that removing an event in a callback no longer throws an exception.
  • Loading branch information
photonstorm committed Feb 5, 2014
1 parent eddce65 commit 68d5c73
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 26 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ Updates:

Bug Fixes:

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


You can view the Change Log for all previous versions at https://github.com/photonstorm/phaser/changelog.md
Expand Down
40 changes: 24 additions & 16 deletions examples/wip/timer simple.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,53 @@

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

function preload() {

game.load.image('mushroom', 'assets/sprites/mushroom2.png');
game.load.image('sonic', 'assets/sprites/pangball.png');
game.load.image('ball', 'assets/sprites/pangball.png');

}

var timer;
var t;

function create() {

game.stage.backgroundColor = '#007236';
game.stage.backgroundColor = '#6688ee';

t = game.time.create(false);

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

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

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

}

function addSprite(key) {
function createBall() {

console.log(arguments);
// A bouncey ball sprite just to visually see what's going on.
var ball = game.add.sprite(game.world.randomX, 0, 'ball');

ball.body.gravity.y = 200;
ball.body.bounce.y = 0.5;
ball.body.collideWorldBounds = true;

console.log('nuked');
game.time.removeAll();
// t.removeAll();

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

}

function update() {
function killThem() {


}

function render() {

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

}
42 changes: 32 additions & 10 deletions src/time/Timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ Phaser.Timer.prototype = {
{
if (this.events[i] === event)
{
this.events.splice(i, 1);
this.events[i].pendingDelete = true;
return true;
}
}
Expand Down Expand Up @@ -308,6 +308,21 @@ Phaser.Timer.prototype = {

this._len = this.events.length;

this._i = 0;

while (this._i < this._len)
{
if (this.events[this._i].pendingDelete)
{
this.events.splice(this._i, 1);
this._len--;
}

this._i++;
}

this._len = this.events.length;

if (this.running && this._now >= this.nextTick && this._len > 0)
{
this._i = 0;
Expand Down Expand Up @@ -371,9 +386,12 @@ Phaser.Timer.prototype = {
*/
pause: function () {

this._pauseStarted = this.game.time.now;
if (this.running && !this.expired)
{
this._pauseStarted = this.game.time.now;

this.paused = true;
this.paused = true;
}

},

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

var pauseDuration = this.game.time.now - this._pauseStarted;

for (var i = 0; i < this.events.length; i++)
if (this.running && !this.expired)
{
this.events[i].tick += pauseDuration;
}
var pauseDuration = this.game.time.now - this._pauseStarted;

this.nextTick += pauseDuration;
for (var i = 0; i < this.events.length; i++)
{
this.events[i].tick += pauseDuration;
}

this.paused = false;
this.nextTick += pauseDuration;

this.paused = false;
}

},

Expand All @@ -405,6 +426,7 @@ Phaser.Timer.prototype = {
this.onComplete.removeAll();
this.running = false;
this.events = [];
this._i = this._len;

}

Expand Down
6 changes: 6 additions & 0 deletions src/time/TimerEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ Phaser.TimerEvent = function (timer, delay, tick, repeatCount, loop, callback, c
*/
this.args = args;

/**
* @property {boolean} pendingDelete - A flag that controls if the TimerEvent is pending deletion.
* @protected
*/
this.pendingDelete = false;

};

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

0 comments on commit 68d5c73

Please sign in to comment.