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:51:28 +00:00
parent 493c12c807
commit db090601b8
6 changed files with 68 additions and 42 deletions
+3 -1
View File
@@ -80,6 +80,7 @@ Significant API changes:
* Time.advancedTiming is a new boolean property. If true Time.fps, fpsMin, fpsMax, frames, msMin and msMax will be calculated, otherwise they remain at their defaults.
* Phaser.StageScaleMode has been renamed to ScaleManager and moved from the system folder to the core folder. It's still available under game.scale.
* If your game references the old Phaser.StageScaleMode consts like SHOW_ALL you need to update them to Phaser.ScaleManager, i.e. Phaser.ScaleManager.SHOW_ALL.
* Time.physicsElapsed is no longer bound or clamped, be wary of this if you use the value anywhere in your code.
New features:
@@ -168,7 +169,8 @@ Bug Fixes:
* Fixed TilemapParser - would spit out a tileset warning if margin/spacing were set (fix #485, thanks Cybolic)
* AnimationParser.spriteSheet wasn't taking the margin or spacing into account when calculating the numbers of sprites per row/column, nor was it allowing for extra power-of-two padding at the end (fix #482, thanks yig)
* AnimationManager.add documentation said that 'frames' could be null, but the code couldn't handle this so it defaults to an empty array if none given (thanks yig)
* Fixed issue stopping SoundManager.volume from working correctly on a global volume basis (fix # 488)
* Fixed issue stopping SoundManager.volume from working correctly on a global volume basis (fixes #488)
* The Timer will no longer create negative ticks during game boot, no matter how small the Timer delay is (fixes #366)
TO DO:
+2 -2
View File
@@ -1,5 +1,5 @@
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.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render }, true);
function preload() {
@@ -15,7 +15,7 @@ var music;
function create() {
game.stage.backgroundColor = '#182d3b';
// game.stage.backgroundColor = '#182d3b';
game.input.touch.preventDefault = false;
music = game.add.audio('boden');
+17 -18
View File
@@ -1,30 +1,29 @@
var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { create: create });
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });
function preload() {
game.load.image('pic', 'assets/pics/backscroll.png');
}
var text;
var b;
var grd;
var last;
var i = 0;
function create() {
game.stage.setBackgroundColor(0x2d2d2d);
//Maybe you have to set it even lower for your machine.
//It's related to the time Phaser needs to boot up.
//The tick stops being negative and behaves nearly normal for me at around 550 - 600ms
text = game.add.text(0, 0, "time");
delay = 250;
foreverTimer = game.time.events.repeat(delay, 10, handleEvent, this);
last = Date.now();
console.log('create started', last);
}
function update() {
function handleEvent() {
console.log('>> Tick', i, 'ms diff:', Date.now() - last);
}
last = Date.now();
function render() {
i++;
}
}
+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;
}
});