phaserjs/phaser/time-and-timers

Claude Code and agent skill by phaserjs/phaser

Trust tier 3
  • community
Source check pending

Checking that the SKILL.md file is still in the repository.

Phaser 4 time: the Scene Clock (this.time), delayedCall, addEvent and the TimerEvent config (delay, repeat, loop, startAt, timeScale, paused), Timelines (this.add.timeline with at, in, from, run, tween, set, sound, event), time scale, pausing, delta time in update, and the FPS limit (fps.limit, game.loop.setFPSLimit). Use when spawning enemies every few seconds, making a countdown or a cooldown, delaying an action, scripting a cutscene or intro, adding slow motion, or making movement frame-rate independent. Corrects v3 habits: this.tweens.timeline() is gone (use this.add.timeline), setTimeout and setInterval ignore pause and time scale, a finished TimerEvent can't be re-added without reset, this.time.timeScale doesn't slow tweens or physics. Triggers on: timer, delayedCall, addEvent, TimerEvent, Clock, Timeline, cutscene, countdown, cooldown, spawn every, slow motion, timeScale, delta, FPS limit.

Install

npx vskill@latest install phaserjs/phaser/time-and-timers

SKILL.md

The same SKILL.md was published earlier as phaserjs/phaser/time-and-timers.

Time and timers

The Scene Clock (this.time) and its Timer Events, Timelines for scripted sequences, time scale, delta time and the FPS limit.

Source: src/time/Clock.js, src/time/TimerEvent.js, src/time/Timeline.js, src/time/typedefs/, src/core/TimeStep.js (the game loop). Related: tweens, scenes (pausing a Scene), game-setup-and-config (the fps config).

Quick start

js
// Once, after 2 seconds
this.time.delayedCall(2000, () => this.scene.start('GameOver'));
// Every 1.5 seconds, forever
const spawner = this.time.addEvent({
delay: 1500,
loop: true,
callback: () => this.add.image(Phaser.Math.Between(50, this.scale.width - 50), -50, 'enemy')
});
// Later
spawner.remove();
  • this.time is the Scene's Clock. Its timers run in the Scene's update step, so they stop while the Scene is paused, follow this.time.timeScale, and are destroyed when the Scene shuts down. setTimeout and setInterval do none of this, so don't use them for game logic.
  • Use arrow functions. In a function callback with no callbackScope, this is the TimerEvent, not the Scene.
  • delayedCall(delay, callback, args, callbackScope) is addEvent with no repeat.

Timer Events

js
const timer = this.time.addEvent({
delay: 500, // ms between fires. Default 0
repeat: 4, // extra fires: 4 fires 5 times. -1 = forever
loop: false, // true = forever, the same as repeat: -1
startAt: 400, // ms already elapsed, so the first fire is 100 ms in
timeScale: 1, // this timer's speed, times this.time.timeScale
paused: false,
args: [ 'tick' ], // passed to the callback
callback: (word) => console.log(word, timer.getRepeatCount())
});
  • addEvent puts the timer in a pending list. It starts on the next frame.
  • delay: 0 with repeat or loop throws TimerEvent infinite loop created via zero delay.
  • timer.delay can be changed while it runs. The new delay is used from the next fire.
  • A long frame can fire a short timer more than once in the same frame.
  • Read it with getProgress() (this fire, 0 to 1), getOverallProgress(), getElapsed(), getRemaining(), getRemainingSeconds(), getOverallRemaining() and getRepeatCount() (repeats left).
  • Stop it with timer.remove(), or timer.remove(true) to fire it one last time. this.time.removeEvent(timer) removes it at once. this.time.removeAllEvents() removes them all.
  • Pause one with timer.paused = true, or all of them with this.time.paused = true.
Reusing a Timer Event

The Clock destroys a timer when it finishes, which clears its callback. To use it again, give reset the full config, then add it:

js
const timer = this.time.addEvent({ delay: 1000, callback: () => console.log('first') });
// Later, once it has finished:
timer.reset({ delay: 1000, repeat: 2, callback: () => console.log('again') });
this.time.addEvent(timer);

reset doesn't add it to the Clock. Since 4.3, addEvent logs a warning when given a destroyed timer, and timer.isDestroyed says whether it has been. Making a new timer with addEvent(config) is usually simpler.

Common patterns

Spawn every N seconds, getting faster
js
const spawner = this.time.addEvent({
delay: 2000,
loop: true,
callback: () =>
{
this.add.image(Phaser.Math.Between(50, this.scale.width - 50), -50, 'enemy');
spawner.delay = Math.max(400, spawner.delay - 100);
}
});
Countdown
js
let seconds = 30;
const label = this.add.text(16, 16, 'Time: 30');
this.time.addEvent({
delay: 1000,
repeat: seconds - 1,
callback: () =>
{
seconds--;
label.setText(`Time: ${seconds}`);
if (seconds === 0)
{
this.scene.start('GameOver');
}
}
});

Or make one delayedCall(30000, ...) and show Math.ceil(timer.getRemainingSeconds()) in update.

Cooldown
js
create ()
{
this.nextShot = 0;
}
update (time)
{
if (this.input.activePointer.isDown && time > this.nextShot)
{
this.nextShot = time + 250;
// fire
}
}

time (the same as this.time.now) is the game loop's time. It doesn't follow this.time.timeScale, or stop while the Clock is paused. For a cooldown that slows with slow motion, use a delayedCall that sets a flag.

Timelines

this.add.timeline(events) runs a script of events at set times: callbacks, tweens, property changes, sounds and custom events. Use it for cutscenes, intros, tutorials and attack patterns. v3's this.tweens.timeline() doesn't exist; use a Timeline, or a Tween Chain for tweens only.

js
const { width, height } = this.scale;
const hero = this.add.sprite(-100, height / 2, 'hero');
const title = this.add.text(width / 2, 100, 'Chapter 1').setOrigin(0.5).setAlpha(0);
const timeline = this.add.timeline([
{ at: 0, tween: { targets: hero, x: width / 2, duration: 1500, ease: 'Sine.easeOut' } },
{ at: 1000, tween: { targets: title, alpha: 1, duration: 500 } },
{ from: 1500, sound: 'chime' }, // 1500 ms after the previous event: 2500
{ from: 500, target: hero, set: { flipX: true } }, // 3000
{ at: 4000, event: 'introdone' }
]);
timeline.on('introdone', () => console.log('intro done'));
timeline.on(Phaser.Time.Events.COMPLETE, () => this.scene.start('Level1'));
timeline.play(); // a Timeline starts paused
KeyWhat it does
atms from the start. Default 0.
inms after the Timeline's current elapsed. For adding events to a running Timeline.
fromms after the previous event in the array. from beats in, which beats at.
targetThe object for set, and this in the callbacks.
setProperties to set on target. Needs target.
tweenA tween config, Tween Chain config or Tween, added to this.tweens. It needs its own targets.
soundA sound key, or { key, config }.
eventEmits this event on the Timeline, with target (or the Timeline) as the argument.
runA callback. this is target, or the Timeline.
loopA callback run on each repeat of the Timeline, but not on its first play.
ifA callback. Return false to skip the event's actions. It still counts as run.
onceRemove the event once it has run, for good. It doesn't come back on reset() or a repeat.
stopStop the Timeline. This emits COMPLETE.

An event's actions run in this order: if, set, loop, tween, sound, event, run, stop.

Control:

  • play() starts from the beginning. pause() and resume() also pause and resume the tweens it started: use resume(), not play(), to carry on.
  • timeline.stop() stops it without emitting COMPLETE. reset() stops the tweens its events started, goes back to the start and plays. clear() removes every event. add(config | array) adds events, even while it runs.
  • repeat() or repeat(-1) repeats forever. repeat(2) plays it 3 times. repeat(false) turns it off. Only -1 means forever.
  • isPlaying(), getProgress() (events run, over all events), elapsed, iteration.
  • timeline.timeScale changes the Timeline's own speed only. It doesn't follow this.time.timeScale, and it doesn't change the tweens it starts: set this.tweens.timeScale for those.

Since 4.3, COMPLETE, and a repeat, wait until every event has run and every tween they started has finished. Tweens which repeat or loop forever aren't waited for. To act the moment the last event runs, give that event a run callback.

A Timeline runs in the Scene's update step, apart from the Clock: this.time.paused doesn't pause it, but pausing the Scene does. It's destroyed when the Scene shuts down.

Slow motion and pausing

Each system keeps its own time, so there's no single switch. For slow motion across a Scene:

js
const scale = 0.25; // quarter speed
this.time.timeScale = scale; // Timer Events
this.tweens.timeScale = scale; // Tweens
this.physics.world.timeScale = 1 / scale; // Arcade Physics: higher is SLOWER
this.anims.globalTimeScale = scale; // sprite animations, in every Scene
  • Also set timeScale on each Timeline and particle emitter you want slowed.
  • Arcade's timeScale is the inverse of the others: 2 is half speed.
  • this.anims is the game's AnimationManager, so globalTimeScale changes every Scene. For one sprite, use sprite.anims.timeScale.
  • this.scene.pause() stops the whole Scene update: timers, tweens, Timelines, physics and animations. It keeps rendering. See scenes.
  • To pause only some systems: this.time.paused = true, this.tweens.pauseAll(), this.physics.pause().

Delta time

js
create ()
{
this.ship = this.add.image(100, this.scale.height / 2, 'ship');
}
update (time, delta)
{
this.ship.x += 200 * (delta / 1000); // 200 px per second, at any frame rate
}
  • Moving a fixed amount each frame (x += 3) runs twice as fast on a 120 Hz screen. Use delta, or use tweens and physics, which already do.
  • delta is in ms and smoothed: it's the average of the last 10 frames (fps.deltaHistory), and a frame longer than 1000 / fps.min ms (200 ms) is replaced by the last normal one, so nothing jumps after a tab switch. The raw value is this.game.loop.rawDelta.
  • this.game.loop.actualFps is the measured frame rate.

FPS limit

js
new Phaser.Game({
width: 1024,
height: 768,
fps: { limit: 30 } // 0 (the default) runs at the display's rate
});
// At runtime (4.2+). 0 removes the limit
this.game.loop.setFPSLimit(60);
  • Use setFPSLimit rather than writing game.loop.fpsLimit, which leaves its derived values stale.
  • fps.target (60) doesn't cap the frame rate. Use limit.
  • Before 4.3, game time ran faster than real time with fps.limit set, such as 1.4 times with a limit of 91 on a 75 Hz display. A game tuned with a limit on 4.2 or earlier may feel slower in 4.3, because it now runs at the right speed.

Gotchas

  • `repeat: 4` fires 5 times. repeat counts the extra fires after the first.
  • `this` in a `function` callback is the TimerEvent. Use an arrow function, or set callbackScope: this.
  • `this.time.timeScale` only scales Timer Events. Tweens, Timelines, physics and animations each have their own time scale. See Slow motion.
  • A Timeline starts paused. Call play().
  • A Timeline tween doesn't use the event's `target`. Give the tween config its own targets.
  • Timers and Timelines don't survive a Scene restart. The Clock destroys its timers on shutdown, and Timelines are destroyed too. Make them again in create.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewPassed

Quality evaluation

  • Task evaluationNot runNo evaluation yet. Run one in Skill Studio.

Source

Popularity

Repository stars
39,633
Repository forks
7,130
7-day trend score
53

Stars and forks are for the whole GitHub repository, not this skill alone. Refreshed 17 May 2026, 6 months ago.

Works with

Any agent vskill installs to: 56 targets in all. These 39 have a page on this site.

  • Claude Code
  • Grok Build (Cursor)
  • GitHub Copilot
  • Windsurf
  • Codex
  • Gemini CLI
  • Cline
  • Amp
  • Roo Code
  • Goose
  • Continue
  • Kilo Code
  • Junie
  • Trae
  • OpenHands
  • Qwen Code
  • AI Agent
  • Tabnine Chat
  • Sourcegraph Cody
  • Amazon Q
  • Firebase Genkit
  • Sweep
  • Devin
  • Phind
  • Mentat
  • Claude Desktop
  • Aider
  • GPT Pilot
  • Plandex
  • bolt.new
  • v0
  • Replit Agent
  • Anse
  • TypingMind
  • BetterChatGPT
  • LibreChat
  • Warp AI
  • Pieces
  • Fig AI
Embed badge
vskill VERIFIED badge
[![vskill VERIFIED](https://verified-skill.com/api/v1/skills/phaserjs/phaser/time-and-timers/badge)](https://verified-skill.com/skills/phaserjs/phaser/time-and-timers)

Something wrong with this skill or its page? Tell us.