Phaser provides a scene model for rendering, animation, audio and input in the browser. TypeScript makes the data boundaries explicit. The important questions are when resources load, which work runs per frame and who disposes listeners when a scene stops. Rendering remote state at a local presentation time builds on those same lifecycle rules.
Responsibilities of preload, create and update
preload queues resources, create builds scene objects from loaded resources, and update performs active per-frame work. Create an image once and update its position or texture frame rather than allocating another image every frame.
A scene can restart. A field initialized in the constructor may have a different lifetime from state that must reset on every create. Old counters, network state and temporary selections should not accidentally become the next session's starting state.
// Independent example; artwork paths are illustrative.
import Phaser from 'phaser';
class DemoScene extends Phaser.Scene {
private actor!: Phaser.GameObjects.Sprite;
preload() {
this.load.spritesheet('actor', '/media/actor.png', {
frameWidth: 64, frameHeight: 64
});
}
create() { this.actor = this.add.sprite(100, 100, 'actor', 0); }
update(_time: number, delta: number) {
this.actor.x += 80 * (delta / 1000);
}
}Texture keys, frame dimensions and atlas geometry
A loader key identifies the resource later used by a sprite. For a regular spritesheet, frameWidth and frameHeight define cell geometry. Image dimensions must agree with that geometry, or frames can appear clipped or offset.
The HTTP URL and texture key are separate identifiers. Reusing a key for different content requires explicit texture-cache handling. Downloading bytes also does not establish that animation definitions reference valid frames. Separate download errors from frame-layout errors when diagnosing visual faults.
Frame duration and a fixed time step
delta is expressed in milliseconds, so pixels-per-second movement divides it by one thousand. Eighty pixels per second advances about 1.33 pixels during 16.67 ms and 2.67 pixels during 33.33 ms. This separates rendering motion from frame count without proving parity with an authoritative remote simulation.
A fixed step accumulates elapsed time and performs updates in stepMs increments. Processing an entire long pause in one frame can cause further delay. The example bounds both incoming elapsed time and consecutive updates, with an explicit policy for discarding overdue work.
let accumulator = 0;
const stepMs = 1000 / 60;
function advance(deltaMs: number, step: () => void) {
accumulator += Math.min(250, Math.max(0, deltaMs));
let count = 0;
while (accumulator >= stepMs && count < 5) {
step(); accumulator -= stepMs; count++;
}
if (accumulator >= stepMs) accumulator %= stepMs;
return accumulator / stepMs;
}From snapshot time to render time
Remote positions can be buffered with their tick. Choose snapshots a and b bracketing the presentation time. Compute (renderTick - a.tick) / (b.tick - a.tick), verify that the tick difference is positive, and clamp the ratio between zero and one.
If x is 1000 at tick 100 and 1400 at tick 102, rendering tick 101 gives 1200. This is a visual estimate, not a new hit or physics result. When no later snapshot exists, holding the last confirmed value is an explicit starvation policy that can be exposed in connection diagnostics.
function interpolateX(
a: {tick: number; x: number},
b: {tick: number; x: number}, renderTick: number
) {
if (b.tick <= a.tick) return a.x;
const t = Math.min(1, Math.max(0,
(renderTick - a.tick) / (b.tick - a.tick)));
return a.x + (b.x - a.x) * t;
}Discrete state and animation time
Positions and velocities can interpolate; facing, health and movement identifiers cannot be averaged meaningfully. Choose them from a snapshot or an explicit transition rule. Smoothing a round reset like ordinary motion can create an unintended glide.
Animation progress also should not depend only on packet arrival. Presentation time can advance within the same move and remain fixed during a visual pause such as hitstop. Those choices should not alter authoritative outcomes. Keeping discrete state separate from draw coordinates makes the boundary easier to inspect.
Dispose resources on shutdown
Stopping a Phaser scene does not automatically dispose every external subscription. A SignalR callback, DOM listener or independent audio object needs an owner. SHUTDOWN is a lifecycle point for removing them and preparing restartable state.
TypeScript interfaces document remote data but do not validate JSON at runtime. Check finite coordinates, valid frame indices and ticks before handing data to rendering. One malformed packet should not become a repeated exception on every update.
Related technical articles
- Supabase Storage and S3: signed HTTP requests, streaming and caching
- SignalR and WebSocket: connections, messaging and reconnection
- MailKit and an outbox: SMTP, TLS and retryable email
- JWT and refresh tokens: validation, rotation and browser sessions
- PostgreSQL and EF Core: DB-first, queries and concurrency