Skip to content

Commit e8869e0

Browse files
committed
Game/Time - updateNumber and updatesThsiFrame
- Changed `count` from 0d9678e to `updateNumber` and expanded documentation; also moved primary usage back to local variable. - Added `updatesThisFrame` which allows (logic) code to detect if it is the last update, or if there are pending updates the same frame. While it could be adventageous in certain cases it will be problematic if such update logic relies in the supplied delta time, as such should change if fixed-timing is deviated from or extended updates are done. - Formatting and documentation.
1 parent e2f65f5 commit e8869e0

1 file changed

Lines changed: 46 additions & 27 deletions

File tree

src/core/Game.js

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -287,46 +287,58 @@ Phaser.Game = function (width, height, renderer, parent, state, transparent, ant
287287
*/
288288
this._codePaused = false;
289289

290+
/**
291+
* The number of the logic update applied this render frame, starting from 0.
292+
*
293+
* The first update is `updateNumber === 0` and the last update is `updateNumber === updatesThisFrame.`
294+
* @property {number} updateNumber
295+
* @protected
296+
*/
297+
this.updateNumber = 0;
298+
299+
/**
300+
* Number of logic updates expected to occur this render frame;
301+
* will be 1 unless there are catch-ups required (and allowed).
302+
* @property {integer} updatesThisFrame
303+
* @protected
304+
*/
305+
this.updatesThisFrame = 1;
306+
290307
/**
291308
* @property {number} _deltaTime - accumulate elapsed time until a logic update is due
292309
* @private
293310
*/
294311
this._deltaTime = 0;
295312

296313
/**
297-
* @property {number} count - Update iteration counter.
298-
* @protected
299-
*/
300-
this.count = 0;
301-
302-
/**
303-
* @property {number} _lastCount - remember how many 'catch-up' iterations were used on the logicUpdate last frame
304-
* @private
305-
*/
314+
* @property {number} _lastCount - remember how many 'catch-up' iterations were used on the logicUpdate last frame
315+
* @private
316+
*/
306317
this._lastCount = 0;
307318

308319
/**
309-
* @property {number} _spiralling - if the 'catch-up' iterations are spiralling out of control, this counter is incremented
310-
* @private
311-
*/
320+
* @property {number} _spiralling - if the 'catch-up' iterations are spiralling out of control, this counter is incremented
321+
* @private
322+
*/
312323
this._spiralling = 0;
313324

314325
/**
315-
* @property {Phaser.Signal} fpsProblemNotifier - if the game is struggling to maintain the desiredFps, this signal will be dispatched
316-
* to suggest that the program adjust it's fps closer to the Time.suggestedFps value
317-
* @public
318-
*/
326+
* If the game is struggling to maintain the desired FPS, this signal will be dispatched.
327+
* The desired/chosen FPS should probably be closer to the {@link Phaser.Time#suggestedFps} value.
328+
* @property {Phaser.Signal} fpsProblemNotifier
329+
* @public
330+
*/
319331
this.fpsProblemNotifier = new Phaser.Signal();
320332

321333
/**
322-
* @property {boolean} forceSingleUpdate - Should the game loop force a logic update, regardless of the delta timer? Set to true if you know you need this. You can toggle it on the fly.
323-
*/
334+
* @property {boolean} forceSingleUpdate - Should the game loop force a logic update, regardless of the delta timer? Set to true if you know you need this. You can toggle it on the fly.
335+
*/
324336
this.forceSingleUpdate = false;
325337

326338
/**
327-
* @property {number} _nextNotification - the soonest game.time.time value that the next fpsProblemNotifier can be dispatched
328-
* @private
329-
*/
339+
* @property {number} _nextNotification - the soonest game.time.time value that the next fpsProblemNotifier can be dispatched
340+
* @private
341+
*/
330342
this._nextFpsNotification = 0;
331343

332344
// Parse the configuration object (if any)
@@ -710,32 +722,39 @@ Phaser.Game.prototype = {
710722

711723
// call the game update logic multiple times if necessary to "catch up" with dropped frames
712724
// unless forceSingleUpdate is true
713-
this.count = 0;
725+
var count = 0;
726+
727+
this.updatesThisFrame = Math.floor(this._deltaTime / slowStep);
728+
if (this.forceSingleUpdate)
729+
{
730+
this.updatesThisFrame = Math.min(1, this.updatesThisFrame);
731+
}
714732

715733
while (this._deltaTime >= slowStep)
716734
{
717735
this._deltaTime -= slowStep;
736+
this.updateNumber = count;
718737
this.updateLogic(1.0 / this.time.desiredFps);
719-
this.count++;
738+
count++;
720739

721-
if (this.forceSingleUpdate && this.count === 1)
740+
if (this.forceSingleUpdate && count === 1)
722741
{
723742
break;
724743
}
725744
}
726745

727746
// detect spiralling (if the catch-up loop isn't fast enough, the number of iterations will increase constantly)
728-
if (this.count > this._lastCount)
747+
if (count > this._lastCount)
729748
{
730749
this._spiralling++;
731750
}
732-
else if (this.count < this._lastCount)
751+
else if (count < this._lastCount)
733752
{
734753
// looks like it caught up successfully, reset the spiral alert counter
735754
this._spiralling = 0;
736755
}
737756

738-
this._lastCount = this.count;
757+
this._lastCount = count;
739758

740759
// call the game render update exactly once every frame unless we're playing catch-up from a spiral condition
741760
this.updateRender(this._deltaTime / slowStep);

0 commit comments

Comments
 (0)