Skip to content

Commit 90a500e

Browse files
committed
Game.update could call updateLogic multiple times in a single frame when catching up with slow device frame rates. This would cause Tweens to advance at twice the speed they should have done (thanks @mkristo)
Time.desiredFpsMult is a pre-calculated multiplier used in Game.update. Time.refresh updates the `Time.time` and `Time.elapsedMS` values and is called automatically by Game.update.
1 parent ee0094f commit 90a500e

4 files changed

Lines changed: 22 additions & 19 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,13 +275,16 @@ If you are an exceptional JavaScript developer and would like to join the Phaser
275275
* Time.desiredFps has moved to a getter / setter.
276276
* Time.physicsElapsed and Time.physicsElapsedMS are no longer calculated every frame, but only when the desiredFps is changed.
277277
* Time.update has been streamlined and the `updateSetTimeout` and `updateRAF` methods merged and duplicate code removed.
278+
* Time.desiredFpsMult is a pre-calculated multiplier used in Game.update.
279+
* Time.refresh updates the `Time.time` and `Time.elapsedMS` values and is called automatically by Game.update.
278280

279281
### Bug Fixes
280282

281283
* Loader.bitmapFont wouldn't automatically set the `atlasURL` value if just the key was given.
282284
* The Loader would put the baseURL and/or path in front of `data:` and `blob` URLs (thanks @rblopes #2044)
283285
* When the Text width was being calculated it would add the `strokeThickness` value twice, causing an alignment offset (thanks @nickryall #2039)
284286
* Sound.onEndedHandler has a fix for AudioBufferSourceNode listener memory leak (thanks @Pappa #2069)
287+
* Game.update could call `updateLogic` multiple times in a single frame when catching up with slow device frame rates. This would cause Tweens to advance at twice the speed they should have done (thanks @mkristo)
285288

286289
For changes in previous releases please see the extensive [Version History](https://github.com/photonstorm/phaser/blob/master/CHANGELOG.md).
287290

src/core/Game.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ Phaser.Game.prototype = {
785785

786786
if (this._kickstart)
787787
{
788-
this.updateLogic(1.0 / this.time.desiredFps);
788+
this.updateLogic(this.time.desiredFpsMult);
789789

790790
// Sync the scene graph after _every_ logic update to account for moved game objects
791791
this.stage.updateTransform();
@@ -805,7 +805,7 @@ Phaser.Game.prototype = {
805805
if (this.time.time > this._nextFpsNotification)
806806
{
807807
// only permit one fps notification per 10 seconds
808-
this._nextFpsNotification = this.time.time + 1000 * 10;
808+
this._nextFpsNotification = this.time.time + 10000;
809809

810810
// dispatch the notification signal
811811
this.fpsProblemNotifier.dispatch();
@@ -842,7 +842,7 @@ Phaser.Game.prototype = {
842842
this._deltaTime -= slowStep;
843843
this.currentUpdateID = count;
844844

845-
this.updateLogic(1.0 / this.time.desiredFps);
845+
this.updateLogic(this.time.desiredFpsMult);
846846

847847
// Sync the scene graph after _every_ logic update to account for moved game objects
848848
this.stage.updateTransform();
@@ -904,7 +904,7 @@ Phaser.Game.prototype = {
904904

905905
this.state.update();
906906
this.stage.update();
907-
this.tweens.update(timeStep);
907+
this.tweens.update();
908908
this.sound.update();
909909
this.input.update();
910910
this.physics.update();

src/time/Time.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ Phaser.Time = function (game) {
9999
*/
100100
this.physicsElapsedMS = 0;
101101

102+
/**
103+
* The desiredFps multiplier as used by Game.update.
104+
* @property {integer} desiredFpsMult
105+
* @protected
106+
*/
107+
this.desiredFpsMult = 1.0 / 60;
108+
102109
/**
103110
* The desired frame rate of the game.
104111
*
@@ -329,6 +336,11 @@ Phaser.Time.prototype = {
329336

330337
},
331338

339+
/**
340+
* Refreshes the Time.time and Time.elapsedMS properties from the system clock.
341+
*
342+
* @method Phaser.Time#refresh
343+
*/
332344
refresh: function () {
333345

334346
// Set to the old Date.now value
@@ -441,7 +453,7 @@ Phaser.Time.prototype = {
441453
this._elapsedAccumulator += this.elapsed;
442454

443455
// occasionally recalculate the suggestedFps based on the accumulated elapsed time
444-
if (this._frameCount >= this.desiredFps * 2)
456+
if (this._frameCount >= this._desiredFps * 2)
445457
{
446458
// this formula calculates suggestedFps in multiples of 5 fps
447459
this.suggestedFps = Math.floor(200 / (this._elapsedAccumulator / this._frameCount)) * 5;
@@ -582,6 +594,8 @@ Object.defineProperty(Phaser.Time.prototype, "desiredFps", {
582594

583595
this.physicsElapsedMS = this.physicsElapsed * 1000;
584596

597+
this.desiredFpsMult = 1.0 / value;
598+
585599
}
586600

587601
});

src/tween/TweenData.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -240,13 +240,6 @@ Phaser.TweenData.prototype = {
240240

241241
this.startTime = this.game.time.time + this.delay;
242242

243-
// DEBUG
244-
this.realStart = Date.now();
245-
this.totalElapsedMs = 0;
246-
247-
// console.log('startTime', this.startTime);
248-
// console.log('realStart', this.realStart);
249-
250243
if (this.parent.reverse)
251244
{
252245
this.dt = this.duration;
@@ -382,13 +375,6 @@ Phaser.TweenData.prototype = {
382375

383376
this.percent = this.dt / this.duration;
384377

385-
this.totalElapsedMs = (this.totalElapsedMs || 0) + this.game.time.elapsedMS;
386-
387-
// if (this.percent === 1)
388-
// {
389-
// console.log("ElapsedTime", this.totalElapsedMs, Date.now() - this.realStart);
390-
// }
391-
392378
this.value = this.easingFunction(this.percent);
393379

394380
for (var property in this.vEnd)

0 commit comments

Comments
 (0)