Skip to content
Tutorials Center
My learning

Topics

Lesson 6 of 36 ยท Game Loop and Scenes

The Phaser Game Loop and Delta Time

Understand the Phaser game loop, update(time, delta), frame-rate independent movement, FPS display, slow motion with time scale and fixed timesteps, with live demos.

Every game, from Pong to a modern blockbuster, runs a loop. Each pass through the loop is called a frame. In a frame the game reads input, updates the world and draws the result. Phaser runs this loop for you, but you must understand it to make movement feel right on every device.

What happens in one frame

Phaser's loop is driven by the browser's requestAnimationFrame, which calls Phaser once before each screen repaint. A typical monitor repaints 60 times per second, so a frame lasts about 16.7 milliseconds. Gaming monitors may repaint 120 or 144 times per second, and a busy phone may drop to 30.

flowchart LR
  A["Browser: next frame"] --> B["Step: update time and delta"]
  B --> C["Scene systems: input, tweens, timers, physics"]
  C --> D["Your scene.update(time, delta)"]
  D --> E["Render all game objects"]
  E --> A

The loop is the same on every device, but how often it runs is not.

Phaser does the middle steps in a fixed order. Physics and tweens move things first, then your update runs, then everything is drawn. This matters when you read a position in update: you see the values after physics for this frame.

The problem: frames are not equal

Suppose you write this:

JavaScript
update() {
  this.player.x += 5;      // 5 pixels per FRAME
}

On a 60 Hz screen the player moves 300 pixels per second. On a 144 Hz screen it moves 720 pixels per second, more than twice as fast. On a slow phone that manages only 30 frames per second it moves 150. The same game would be unplayable on one of them. Movement tied to frames is the single most common beginner mistake in games.

The fix: multiply by delta time

Phaser passes two numbers to update:

  • time: milliseconds since the game started (a growing total);
  • delta: milliseconds since the previous frame (about 16.7 at 60 FPS, 6.9 at 144 FPS).

To move at a speed measured in pixels per second, multiply the speed by the seconds that passed:

JavaScript
update(time, delta) {
  const seconds = delta / 1000;
  this.player.x += 200 * seconds;      // 200 pixels per SECOND on any device
}

At 60 FPS that adds 3.33 pixels per frame; at 144 FPS it adds 1.39. After one second both have moved 200 pixels. This idea, rate times elapsed time, applies to everything that changes continuously: movement, rotation, fading, cooldown timers, health regeneration.

The runnable example below moves a square across the screen in two ways. The top one uses delta time. The bottom one is the wrong, per-frame method. To show the difference clearly, the game is limited to 20 frames per second on purpose by setting fps: { limit: 20 }. The log tells you how far the delta-based square went in about one second.

Delta time versus per-frame movement

The green square always travels about 120 pixels in a second. The red one moves 2 pixels per frame, so at 20 FPS it goes 40 pixels in a second, but at 60 FPS it would go 120. Remove the fps limit and rerun to see the red number change while the green number stays put.

Physics is already handled. Arcade Physics bodies use their own time step internally, and tweens use the clock, so you do not multiply anything when you set body.setVelocityX(200). You need delta time in your own per-frame code that changes positions, angles or counters directly.

Measuring FPS

this.game.loop is the object that runs the loop. Useful readings:

  • this.game.loop.actualFps: smoothed frames per second right now;
  • this.game.loop.delta: the current delta in ms;
  • this.game.loop.targetFps: the configured target (60 by default).

Phaser can also show a live counter in your own text object, as below. The demo adds a button-like text that toggles slow motion by scaling the delta you use for the animation. A game-wide timeScale is a favorite trick for hit-stop effects (freeze for a tenth of a second when something is hit) and "bullet time".

FPS meter and slow motion

Click the text and the ball slows to a quarter of its speed. Because all the movement goes through dt, one number controls the whole feel.

Big deltas and the tab-switch problem

If the player switches to another browser tab, the browser pauses the loop. When they come back, the first frame may have a delta of several seconds. If you multiply speed by such a delta, objects teleport across the world or through walls. Phaser protects you by smoothing the delta and capping the step, and Arcade Physics steps at a fixed rate, but your own code should clamp too:

JavaScript
update(time, delta) {
  const dt = Math.min(delta, 50) / 1000;   // never simulate more than 50 ms in one step
  this.enemy.x += this.enemySpeed * dt;
}

Fixed timestep for exact simulations

Some logic must behave the same regardless of frame rate, for example a deterministic puzzle or a replay. For those, use a fixed timestep with an accumulator: collect time, then run the update in equal slices.

JavaScript
const STEP = 1000 / 60;                // one logic tick = 16.67 ms

update(time, delta) {
  this.acc += Math.min(delta, 250);
  while (this.acc >= STEP) {
    this.tick(STEP / 1000);            // your logic always sees the same dt
    this.acc -= STEP;
  }
}

On a fast screen several frames pass without a tick; on a slow one a frame may run two or three ticks. Arcade Physics already works this way internally (its fps option sets the tick rate, 60 by default), which is why physics stays stable. For ordinary games, plain delta movement is enough.

Other time tools

  • this.time.now is the scene clock in ms.
  • this.time.delayedCall(ms, fn) runs a function once later; this.time.addEvent({ delay, loop: true, callback }) repeats it.
  • this.time.timeScale = 0.5 slows every timer event of the scene; this.tweens.timeScale slows tweens; this.physics.world.timeScale slows Arcade Physics (a larger value means slower).
  • this.game.loop.sleep() and wake() pause the whole loop, which is rarely needed.

Do not use a bare setInterval for game logic: it keeps running while the game is paused or the scene is over.

Cooldowns and timers the delta way

A cooldown is a counter you decrement:

JavaScript
// in create
this.shootCooldown = 0;

// in update(time, delta)
this.shootCooldown -= delta;
if (this.shootCooldown <= 0 && fireKeyIsDown) {
  this.fire();
  this.shootCooldown = 250;      // can fire again after 250 ms
}

Or compare against the clock: if (time > this.nextShot) { fire; this.nextShot = time + 250; }. Both are frame-rate independent.

Common mistakes

  • Moving by a fixed amount per frame. Always scale by delta / 1000 for your own movement.
  • Mixing units. delta is in milliseconds; speeds are normally in pixels per second, so divide by 1000.
  • Heavy work in update. If it takes longer than about 10 ms, the frame rate suffers. Avoid allocations and big loops over every object.
  • Not clamping delta. A long pause can produce a huge step; use Math.min(delta, 50).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A game moves a sprite with this.x += 5 in update(). What is the problem?

    Adding a fixed amount per frame makes a 144 Hz screen run the game much faster than a 60 Hz one. Multiply by delta time instead.
  2. 2. What is the correct way to move 200 pixels per second in update(time, delta)?

    delta is in milliseconds, so delta / 1000 is seconds; speed times seconds gives the distance for this frame.
  3. 3. What does the delta argument of update() contain?

    delta is the duration of the last frame in milliseconds; time is the running total.
  4. 4. Why clamp delta with Math.min(delta, 50) in your own movement code?

    After a pause the first delta can be seconds long. Clamping it keeps movement small so objects do not teleport through walls.
  5. 5. Which tool should you use to run a repeating game event that stops when the scene stops?

    Scene timers are tied to the scene lifecycle and the game clock, unlike setInterval.

Summary

  • Each frame Phaser updates systems, calls update(time, delta) and renders; the number of frames per second varies by device.
  • Multiply your own per-frame changes by delta / 1000 so speeds are in units per second.
  • Physics and tweens already handle time themselves; clamp huge deltas in your own code.
  • this.game.loop.actualFps measures speed; a scaled dt gives slow motion and hit-stop effects.
  • Use a fixed timestep accumulator when logic must be exactly repeatable; use this.time for timers.

Next: Loading Assets and Generating Textures