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.
This commit is contained in:
photonstorm
2014-02-05 22:35:35 +00:00
parent eddce653e9
commit 68d5c73fea
4 changed files with 65 additions and 27 deletions
+2
View File
@@ -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
+24 -16
View File
@@ -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);
}
+33 -11
View File
@@ -245,7 +245,7 @@ Phaser.Timer.prototype = {
{
if (this.events[i] === event)
{
this.events.splice(i, 1);
this.events[i].pendingDelete = true;
return true;
}
}
@@ -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;
@@ -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;
}
},
@@ -383,17 +401,20 @@ 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;
for (var i = 0; i < this.events.length; i++)
{
this.events[i].tick += pauseDuration;
}
this.nextTick += pauseDuration;
this.paused = false;
}
this.nextTick += pauseDuration;
this.paused = false;
},
/**
@@ -405,6 +426,7 @@ Phaser.Timer.prototype = {
this.onComplete.removeAll();
this.running = false;
this.events = [];
this._i = this._len;
}
+6
View File
@@ -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;