phaserjs/phaser/events-system

Claude Code and agent skill by phaserjs/phaser

Trust tier 2
  • community
LLM review pending
Source check pending

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

Phaser 4 events: EventEmitter (on, once, off, emit, context), which object emits which events, the event constants (Phaser.Scenes.Events, Phaser.Core.Events, Phaser.Input.Events, Phaser.Loader.Events and the rest) and where each set lives in src, per-key events such as keydown-SPACE and filecomplete-image-key, custom events and an event bus between Scenes, and removing listeners on SHUTDOWN. Use when wiring a HUD to the game Scene, pausing on blur or a hidden tab, reacting to resize, game over, or a listener firing twice after scene.restart(). Corrects v3 habits and common bugs: listeners on this.events, this.game.events, this.registry, this.scale and other Scenes leak across restarts; a mistyped event string never fires; removeAllListeners on this.events breaks the Scene. Triggers on: events, on, once, off, emit, EventEmitter, listener, event bus, SHUTDOWN, cleanup, memory leak, custom event.

Install

npx vskill@latest install phaserjs/phaser/events-system

SKILL.md

The same SKILL.md was published earlier as phaserjs/phaser/events-system.

Events

Phaser's systems are event emitters, built on eventemitter3. This covers how to listen, which object emits what, where the event constants live, and how to remove listeners so a Scene restart doesn't duplicate them.

Source: src/events/EventEmitter.js, and the events/ folder of each system (table below). Related: phaser4-core, scenes, data-manager, input-keyboard-mouse-touch.

Quick start

js
create ()
{
// Listen with a constant, and pass the Scene as the context
this.input.on(Phaser.Input.Events.POINTER_DOWN, this.onPointerDown, this);
// Once, then it removes itself
this.cameras.main.once(Phaser.Cameras.Scene2D.Events.FADE_OUT_COMPLETE, () => this.scene.start('GameOver'));
// A custom event on the Scene's own emitter
this.events.on('player-died', this.onPlayerDied, this);
// A Phaser event on an emitter which outlives the Scene
this.game.events.on(Phaser.Core.Events.BLUR, this.onBlur, this);
// this.events and this.game.events keep their listeners when the Scene restarts: remove yours on SHUTDOWN
this.events.once(Phaser.Scenes.Events.SHUTDOWN, () =>
{
this.events.off('player-died', this.onPlayerDied, this);
this.game.events.off(Phaser.Core.Events.BLUR, this.onBlur, this);
});
}
onPointerDown (pointer, currentlyOver) { this.events.emit('player-died', 1200); }
onPlayerDied (score) { this.cameras.main.fadeOut(500); }
onBlur () { this.scene.pause(); }

The EventEmitter

Phaser.Events.EventEmitter is eventemitter3 with shutdown() and destroy() added, both of which call removeAllListeners().

  • on(event, fn, context?) and once(...) return the emitter, so they chain. addListener is on.
  • off(event, fn?, context?, once?) (also removeListener): with fn it removes only that function, and with context only for that context. Without `fn` it removes every listener for the event, including ones other Scenes added.
  • emit(event, ...args) returns true if anything was listening. Listeners run in the order they were added, synchronously.
  • listenerCount(event), listeners(event), eventNames(), removeAllListeners(event?).
  • The third argument sets this in a normal function. An arrow function keeps the this of where it was written and ignores it.
  • off needs the same function to find a listener. An inline arrow function can't be removed later, so use a method, or keep the function in a variable.
  • A Game Object is an emitter, so sprite.on('pointerdown', ...) and sprite.emit('hit') work. Its listeners go when it is destroyed.

Which object emits what

Every built-in event has a constant whose value is the string, such as Phaser.Input.Events.POINTER_DOWN === 'pointerdown'. The constants, with the arguments each event passes, are one file per event in these folders:

ConstantsFolder in src/Emitted byCleared when the Scene shuts down?
Phaser.Scenes.Eventsscene/events/this.eventsNo, kept across restarts
Phaser.Core.Eventscore/events/this.game.eventsNo
Phaser.Input.Eventsinput/events/this.input, and interactive Game ObjectsYes
this.input.manager.events (POINTERLOCK_CHANGE, MANAGER_*)No
Phaser.Input.Keyboard.Eventsinput/keyboard/events/this.input.keyboard, and each KeyYes
Phaser.Input.Gamepad.Eventsinput/gamepad/events/this.input.gamepad, and each GamepadYes
Phaser.Loader.Eventsloader/events/this.loadYes
Phaser.Cameras.Scene2D.Eventscameras/2d/events/each CameraYes, the Cameras are destroyed
Phaser.GameObjects.Eventsgameobjects/events/each Game Object, and Video (VIDEO_*)Yes, they are destroyed
Phaser.GameObjects.Particles.Eventsgameobjects/particles/events/each ParticleEmitterYes
Phaser.Animations.Eventsanimations/events/this.anims (ADD_ANIMATION, PAUSE_ALL...), and each Sprite (ANIMATION_*)this.anims: No
Phaser.Tweens.Eventstweens/events/each TweenYes, the Tweens are destroyed
Phaser.Time.Eventstime/events/each Timeline (COMPLETE). A TimerEvent isn't an emitter: use its callbackYes, the Timelines are destroyed
Phaser.Sound.Eventssound/events/this.sound (GLOBAL_*, DECODED, UNLOCKED...), and each SoundNo
Phaser.Physics.Arcade.Eventsphysics/arcade/events/this.physics.worldYes
Phaser.Physics.Matter.Eventsphysics/matter-js/events/this.matter.worldYes
Phaser.Data.Eventsdata/events/the owner of the data: a Game Object, this.events (for this.data) or this.registry.eventsGame Object: yes. this.events and the registry: No
Phaser.Scale.Eventsscale/events/this.scaleNo
Phaser.Textures.Eventstextures/events/this.texturesNo
Phaser.Cache.Eventscache/events/each cache's events, such as this.cache.json.eventsNo
Phaser.Renderer.Eventsrenderer/events/this.rendererNo
Phaser.Structs.Eventsstructs/events/a ProcessQueue (internal)

The same string is used by several systems: 'complete', 'start', 'update', 'pause', 'destroy', 'add'. Listening on the wrong emitter does nothing and reports nothing, which is one more reason to use the constants: Phaser.Tweens.Events.TWEEN_COMPLETE makes it clear the listener belongs on a Tween.

Since 4.3 the constants have literal types in TypeScript, such as 'pointerdown' rather than string.

Scene and Game events

Each frame a Scene emits PRE_UPDATE, UPDATE, then calls its update(time, delta), then POST_UPDATE. When it renders it emits PRE_RENDER and then RENDER, after its Cameras have drawn. The lifecycle events are BOOT (once), START, READY, CREATE, PAUSE, RESUME, SLEEP, WAKE, SHUTDOWN, DESTROY and the TRANSITION_* events. See scenes.

this.game.events emits BLUR and FOCUS (the window), HIDDEN and VISIBLE (the tab), PAUSE and RESUME (the game loop), PRE_STEP, STEP, POST_STEP, PRE_RENDER, POST_RENDER, CONTEXT_LOST, READY, BOOT and DESTROY.

Per-key events

Some events have a prefix constant, and the event is the prefix plus a name:

js
const boom = this.add.sprite(300, 300, 'explosion');
this.input.keyboard.on(Phaser.Input.Keyboard.Events.KEY_DOWN + 'SPACE', () => boom.play('explode')); // 'keydown-SPACE'
boom.once(Phaser.Animations.Events.ANIMATION_COMPLETE_KEY + 'explode', () => boom.destroy()); // 'animationcomplete-explode'
this.load.on(Phaser.Loader.Events.FILE_KEY_COMPLETE + 'image-logo', (key, type, data) => {}); // 'filecomplete-<type>-<key>'

The others are Phaser.Input.Keyboard.Events.KEY_UP, Phaser.Textures.Events.ADD_KEY and REMOVE_KEY ('addtexture-<key>'), and Phaser.Data.Events.CHANGE_DATA_KEY ('changedata-<key>'). The name keeps its case. Key names are the upper case Phaser.Input.Keyboard.KeyCodes names, such as 'A', 'SPACE' and 'LEFT'.

Removing listeners on SHUTDOWN

A Scene stops or restarts with SHUTDOWN, and only fires DESTROY when it is removed from the game. What is cleared on shutdown is in the table above. Everything marked No keeps its listeners, and create adds them again on the next start, so the handler runs twice, then three times, and keeps the old Scene alive.

js
create ()
{
this.scale.on(Phaser.Scale.Events.RESIZE, this.onResize, this);
this.registry.events.on(Phaser.Data.Events.CHANGE_DATA_KEY + 'lives', this.onLives, this);
this.events.on(Phaser.Scenes.Events.POST_UPDATE, this.lateUpdate, this);
// once, not on: this.events keeps its listeners, so an `on` would be added again each start
this.events.once(Phaser.Scenes.Events.SHUTDOWN, () =>
{
this.scale.off(Phaser.Scale.Events.RESIZE, this.onResize, this);
this.registry.events.off(Phaser.Data.Events.CHANGE_DATA_KEY + 'lives', this.onLives, this);
this.events.off(Phaser.Scenes.Events.POST_UPDATE, this.lateUpdate, this);
});
}
onResize (gameSize) { }
onLives (game, value) { }
lateUpdate (time, delta) { }
  • Never call this.events.removeAllListeners(), or off(event) without a function, on a Scene. The Scene's own systems, such as the Clock, Tweens, Cameras and the update list, listen on this.events, and the Scene stops working.

Custom events and an event bus

For events inside one Scene, emit on this.events with your own names. For events between Scenes, use one shared emitter:

js
// events.js: one emitter for the whole game, and the names it uses
export const bus = new Phaser.Events.EventEmitter();
export const GameEvents = { SCORE: 'score', PLAYER_DIED: 'player-died' };
// In the HUD Scene
create ()
{
bus.on(GameEvents.SCORE, this.setScore, this);
this.events.once(Phaser.Scenes.Events.SHUTDOWN, () => bus.off(GameEvents.SCORE, this.setScore, this));
}
setScore (score) { }
// In the game Scene
bus.emit(GameEvents.SCORE, 100);

this.game.events works as a bus too, but its names can clash with Phaser's own game events, such as 'pause'. For values rather than one-off events, the registry is often simpler: see data-manager.

Gotchas

  • Arcade Physics `COLLIDE`, `OVERLAP`, `TILE_COLLIDE`, `TILE_OVERLAP` and `WORLD_BOUNDS` only fire for bodies which ask for them: set body.onCollide, body.onOverlap or body.onWorldBounds to true. Colliders also take a callback.
  • `POINTERLOCK_CHANGE` is emitted on `this.input.manager.events`, not this.input.
  • Sound Manager events are `GLOBAL_MUTE` (`'mute'`), `GLOBAL_VOLUME` (`'volume'`) and so on, on this.sound. DECODED and DECODED_ALL are emitted by the Sound Manager, not a Sound.
  • The Animation Manager emits `ADD_ANIMATION` as `'add'`, not 'addanimation'.
  • `Phaser.Time.Events.COMPLETE` is for a Timeline. Since 4.3 it waits until the Tweens the Timeline started have finished.
  • New in 4.3: Phaser.Loader.Events.FILE_LOAD_RETRY ('loadretry', with file, event, retries), and the Scale Manager now emits Phaser.Scale.Events.FULLSCREEN_FAILED when the browser refuses fullscreen.
  • An error thrown in a listener stops the listeners after it for that emit, and propagates to the code which called emit.

Security

  • Security scanPassed, 100 of 100Pattern checks on the skill's files.
  • Model reviewNot runA model review is scheduled for the next rescan.

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/events-system/badge)](https://verified-skill.com/skills/phaserjs/phaser/events-system)

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