diff --git a/README.md b/README.md index 152278d4..6e10445f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/wip/timer simple.js b/examples/wip/timer simple.js index f7b85429..bee855ad 100644 --- a/examples/wip/timer simple.js +++ b/examples/wip/timer simple.js @@ -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); } diff --git a/src/time/Timer.js b/src/time/Timer.js index ee170f9c..410c41bf 100644 --- a/src/time/Timer.js +++ b/src/time/Timer.js @@ -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; } diff --git a/src/time/TimerEvent.js b/src/time/TimerEvent.js index 9bc152c5..1acd75d8 100644 --- a/src/time/TimerEvent.js +++ b/src/time/TimerEvent.js @@ -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;