Phaser 4 Core
Set up the foundation of a Phaser game: the Game config, the Scene
lifecycle, asset loading, cameras, and passing data between scenes. Targets
Phaser 4.2 for new projects; keep an existing Phaser 3.90 project on its
pinned major unless the user explicitly asks for a migration.
When to use
- Use when starting a Phaser game, wiring the
Phaser.Gameconfig, structuringScenes, loading assets inpreload, or fixing scene transitions and shared state. - Use when the project has
phaserinpackage.jsonorimport Phaser from 'phaser', and code usespreload()/create()/update().
When not to use: movement, velocity, colliders, gravity, or overlap → use
phaser-arcade-physics. Complex rigid-body simulation uses Matter physics (a
separate concern). For cross-engine save/load patterns use save-systems.
Core workflow
- Detect the installed major first. Read
package.jsonand the lockfile. Use Phaser 4.2 for new work; do not silently rewrite a Phaser 3 project as Phaser 4. - Create the game from a config.
new Phaser.Game(config)withtype: Phaser.AUTO(WebGL with Canvas fallback), awidth/height, and ascenearray. The first scene (and any withactive: true) starts automatically. - Model each screen as a
Scene. SubclassPhaser.Scene, pass a uniquekeytosuper, and implement the lifecycle:init(data)→preload()→create(data)→update(time, delta). - Load assets in
preload, use them increate. Queued assets are not available untilcreate. The loader is per-scene; the cache it fills is global. - Reset per-run state in
init(), not the constructor. A scene instance is reused across restarts, so constructor-set fields keep stale values. - Move between screens with
this.scene.start/launch/switch/sleep/wake. Share data throughthis.registry(global) or a sibling scene's event emitter. - Run and observe. Serve the page, open it, and confirm assets load (watch the Network tab and console) and scenes switch as expected before assuming success.
Patterns
1. Game config + boot (ES module)
2. A Scene with the full lifecycle
3. Cross-scene data + events
4. Scene transitions (pick the right verb)
5. A camera that follows the player
Pitfalls
- Assets are
undefinedincreate/update→ you forgot to queue them inpreload, or used the wrong key. The loader runs betweenpreloadandcreate. - State leaks across a restart → you set fields in the constructor. The Scene
instance is reused; reset run state in
init()and clear arrays onshutdown. this.scene.startvsthis.scene.launch→startstops the calling scene;launchruns the target alongside it. Usingstartfor a HUD hides the game.thisis wrong in a callback → arrow functions keep the Scene'sthis; plainfunctioncallbacks need a context argument or.bind(this).- Phaser 2 tutorials don't work → "States" were renamed to "Scenes" in Phaser 3, and each Scene owns its own systems (input, cameras, tweens) rather than a global Game World.
- Phaser 3 custom pipelines fail in Phaser 4 → Phaser 4 rebuilt the renderer and replaced the old FX/pipeline extension points. Migrate custom shaders and renderer plugins against the Phaser 4 guide; do not mechanically copy internal renderer code.
- Nothing renders / black screen → confirm the canvas mounted,
width/heightare set, and a scene actually started (checkgame.scene.dump()output).
References
- For the full scene state machine (pause/resume vs sleep/wake vs stop/start, the
restart-state bug, and removing/replacing scenes), read
references/scene-flow.md.
Related skills
phaser-arcade-physics— velocity, gravity, colliders, overlap, and groups.input-systems— rebindable, multi-device input architecture (engine-agnostic).pixijs-rendering/threejs-scene-setup— other browser rendering stacks.platformer/puzzle— genre templates that compose Phaser skills.
