The Timer will no longer create negative ticks during game boot, no matter how small the Timer delay is (fixes #366)

This commit is contained in:
photonstorm
2014-02-26 19:52:23 +00:00
parent 493c12c807
commit db090601b8
6 changed files with 68 additions and 42 deletions
+1 -1
View File
@@ -107,7 +107,7 @@ Phaser.RequestAnimationFrame.prototype = {
*/
updateRAF: function (time) {
this.game.update(time);
this.game.update(Date.now());
this._timeOutID = window.requestAnimationFrame(this._onLoop);
+15 -8
View File
@@ -153,7 +153,10 @@ Phaser.Time = function (game) {
Phaser.Time.prototype = {
/**
* Called automatically by Phaser.Game after boot. Should not be called directly.
*
* @method Phaser.Time#boot
* @protected
*/
boot: function () {
@@ -163,6 +166,7 @@ Phaser.Time.prototype = {
/**
* Creates a new stand-alone Phaser.Timer object.
*
* @method Phaser.Time#create
* @param {boolean} [autoDestroy=true] - A Timer that is set to automatically destroy itself will do so after all of its events have been dispatched (assuming no looping events).
* @return {Phaser.Timer} The Timer object that was created.
@@ -181,6 +185,7 @@ Phaser.Time.prototype = {
/**
* Remove all Timer objects, regardless of their state.
*
* @method Phaser.Time#removeAll
*/
removeAll: function () {
@@ -195,8 +200,10 @@ Phaser.Time.prototype = {
},
/**
* Updates the game clock and calculate the fps. This is called automatically by Phaser.Game.
* Updates the game clock and if enabled the advanced timing data. This is called automatically by Phaser.Game.
*
* @method Phaser.Time#update
* @protected
* @param {number} time - The current timestamp, either performance.now or Date.now depending on the browser.
*/
update: function (time) {
@@ -242,12 +249,6 @@ Phaser.Time.prototype = {
this.physicsElapsed = 1.0 * (this.elapsed / 1000);
// Clamp the delta
if (this.physicsElapsed > 0.05)
{
this.physicsElapsed = 0.05;
}
// Paused but still running?
if (!this.game.paused)
{
@@ -277,6 +278,7 @@ Phaser.Time.prototype = {
/**
* Called when the game enters a paused state.
*
* @method Phaser.Time#gamePaused
* @private
*/
@@ -304,6 +306,7 @@ Phaser.Time.prototype = {
/**
* Called when the game resumes from a paused state.
*
* @method Phaser.Time#gameResumed
* @private
*/
@@ -328,8 +331,9 @@ Phaser.Time.prototype = {
/**
* The number of seconds that have elapsed since the game was started.
*
* @method Phaser.Time#totalElapsedSeconds
* @return {number}
* @return {number} The number of seconds that have elapsed since the game was started.
*/
totalElapsedSeconds: function() {
return (this.now - this._started) * 0.001;
@@ -337,6 +341,7 @@ Phaser.Time.prototype = {
/**
* How long has passed since the given time.
*
* @method Phaser.Time#elapsedSince
* @param {number} since - The time you want to measure against.
* @return {number} The difference between the given time and now.
@@ -347,6 +352,7 @@ Phaser.Time.prototype = {
/**
* How long has passed since the given time (in seconds).
*
* @method Phaser.Time#elapsedSecondsSince
* @param {number} since - The time you want to measure (in seconds).
* @return {number} Duration between given time and now (in seconds).
@@ -357,6 +363,7 @@ Phaser.Time.prototype = {
/**
* Resets the private _started value to now.
*
* @method Phaser.Time#reset
*/
reset: function () {
+30 -12
View File
@@ -143,7 +143,14 @@ Phaser.Timer.prototype = {
if (this.running)
{
tick += this._now;
if (this._now === 0)
{
tick += this.game.time.now;
}
else
{
tick += this._now;
}
}
var event = new Phaser.TimerEvent(this, delay, tick, repeatCount, loop, callback, callbackContext, args);
@@ -216,7 +223,7 @@ Phaser.Timer.prototype = {
* Starts this Timer running.
* @method Phaser.Timer#start
*/
start: function() {
start: function () {
this._started = this.game.time.now;
this.running = true;
@@ -227,7 +234,7 @@ Phaser.Timer.prototype = {
* Stops this Timer from running. Does not cause it to be destroyed if autoDestroy is set to true.
* @method Phaser.Timer#stop
*/
stop: function() {
stop: function () {
this.running = false;
this.events.length = 0;
@@ -239,7 +246,7 @@ Phaser.Timer.prototype = {
* @param {Phaser.TimerEvent} event - The event to remove from the queue.
* @method Phaser.Timer#remove
*/
remove: function(event) {
remove: function (event) {
for (var i = 0; i < this.events.length; i++)
{
@@ -297,14 +304,17 @@ Phaser.Timer.prototype = {
* @param {number} time - The time from the core game clock.
* @return {boolean} True if there are still events waiting to be dispatched, otherwise false if this Timer can be destroyed.
*/
update: function(time) {
update: function (time) {
if (this.paused)
{
return true;
}
this._now = time - this._started;
// this._now = time - this._started;
this._now = time;
// console.log('Timer update', this._now, time);
this._len = this.events.length;
@@ -331,15 +341,23 @@ Phaser.Timer.prototype = {
{
if (this._now >= this.events[this._i].tick)
{
var diff = this._now - this.events[this._i].tick;
var newTick = (this._now + this.events[this._i].delay) - diff;
if (newTick < 0)
{
newTick = this._now + this.events[this._i].delay;
}
if (this.events[this._i].loop === true)
{
this.events[this._i].tick += this.events[this._i].delay - (this._now - this.events[this._i].tick);
this.events[this._i].tick = newTick;
this.events[this._i].callback.apply(this.events[this._i].callbackContext, this.events[this._i].args);
}
else if (this.events[this._i].repeatCount > 0)
{
this.events[this._i].repeatCount--;
this.events[this._i].tick += this.events[this._i].delay - (this._now - this.events[this._i].tick);
this.events[this._i].tick = newTick;
this.events[this._i].callback.apply(this.events[this._i].callbackContext, this.events[this._i].args);
}
else
@@ -418,10 +436,10 @@ Phaser.Timer.prototype = {
},
/**
* Destroys this Timer. Events are not dispatched.
* Destroys this Timer. Any pending Events are not dispatched.
* @method Phaser.Timer#destroy
*/
destroy: function() {
destroy: function () {
this.onComplete.removeAll();
this.running = false;
@@ -488,7 +506,7 @@ Object.defineProperty(Phaser.Timer.prototype, "length", {
Object.defineProperty(Phaser.Timer.prototype, "ms", {
get: function () {
return this._now;
return this._now - this._started;
}
});
@@ -501,7 +519,7 @@ Object.defineProperty(Phaser.Timer.prototype, "ms", {
Object.defineProperty(Phaser.Timer.prototype, "seconds", {
get: function () {
return this._now * 0.001;
return this.ms * 0.001;
}
});