melonjs-events
What it does
It explains how to listen for game events, such as frame updates and level loads, and how to stop listening. It is for game developers who need reliable event handling.
When to use it
- βWhen a developer wants code to run on every frame or when a level finishes loading.
- βWhen a developer's game slows down over time because old event listeners are still active.
- βWhen a developer wants a custom event for their own game logic.
Skill link
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-events/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agentβs skills folder.
Example of what to ask your AI
βRun my code when a level finishes loading in melonJS, and make sure I can remove the listener later.β
5 new skills a week. 1 minute to read.
How it works
The skill tells the AI to keep a reference to each handler or use the context argument, so the same handler can be removed later.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
Events and lifecycle
The bus
import { event } from "melonjs";
const onUpdate = () => { /* β¦ */ };
const unsubscribe = event.on(event.GAME_UPDATE, onUpdate); // on() returns its own off()
event.once(event.LEVEL_LOADED, () => { /* β¦ */ });
// two ways to undo it β the same reference, or the returned function
event.off(event.GAME_UPDATE, onUpdate);
unsubscribe();
event.on/once/off/has all take an optional third context argument,
invoked as the handler's this. off matches on the pair β handler identity
AND context identity. on(EVT, fn, this) followed by off(EVT, fn) removes
nothing, silently.
Store the handler reference, or use the context argument. An inline arrow or
a fresh .bind(this) in the on call can never be removed β that is the
standard way to leak listeners in melonJS.
// β unremovable
event.on(event.GAME_UPDATE, this.tick.bind(this));
// β keep the bound reference
this._tick = this.tick.bind(this);
event.on(event.GAME_UPDATE, this._tick);
// later: event.off(event.GAME_UPDATE, this._tick);
// β or skip the bind entirely β the context is part of the identity
event.on(event.GAME_UPDATE, this.tick, this);
// later: event.off(event.GAME_UPDATE, this.tick, this);
Custom event names work at runtime (event.emit("my.custom.event", payload)),
but on/emit are typed against a closed map of the engine's own events, so in
TypeScript a name outside that map is a compile error. Use your own emitter for
game events in a .ts project.
The events worth knowing
Per frame
| event | when |
|---|---|
GAME_BEFORE_UPDATE / GAME_UPDATE / GAME_AFTER_UPDATE |
around the world update β GAME_UPDATE is not emitted while paused |
GAME_BEFORE_DRAW / GAME_AFTER_DRAW |
around the draw pass |
WORLD_STEP |
after the physics step |
TICK |
the raw engine tick |
Lifecycle
| event | when |
|---|---|
BOOT, GAME_INIT, VIDEO_INIT |
engine start-up |
STATE_CHANGE, STATE_PAUSE, STATE_RESUME, STATE_STOP |
state machine |
STAGE_RESET |
a stage was reset |
LEVEL_LOADED |
a Tiled level finished loading |
LOADER_PROGRESS, LOADER_COMPLETE, LOADER_ERROR |
asset loading |
BLUR / FOCUS |
the page lost or regained focus |
CANVAS_ONRESIZE, VIEWPORT_ONRESIZE |
relayout hooks |
POINTERMOVE |
global pointer movement |
Use LOADER_PROGRESS / LOADER_COMPLETE. The loader.onProgress /
loader.onload / loader.onError properties were deprecated in 18.2.0 and
removed in 20.3: they were module-level let bindings on an ES module
namespace, so loader.onProgress = fn threw and the documented migration path
never worked. For completion, loader.preload(assets, onloadcb) also takes the
callback directly.
Renderable lifecycle callbacks
Prefer these over bus subscriptions where they fit β the engine calls them at the right times and there is nothing to unsubscribe:
| callback | when |
|---|---|
onActivateEvent() |
addChild-ed into a container that is already attached to the world root (a Container forwards it to its children) |
onDeactivateEvent() |
removed from its container β including the automatic world clear on a state change |
onDestroyEvent() |
destroy() ran; called with no arguments from the container-removal path |
onCollisionStart / onCollisionActive / onCollisionEnd |
contacts |
Note the activate gate: adding a child to a detached container fires nothing.
The callback arrives later, when that container itself is added to the world.
And an isPersistent renderable survives the world clear between stages, so it
gets no fresh onActivateEvent on the next state.
Pair subscriptions with activate/deactivate, not the constructor. This is the rule that keeps pooled objects working:
onActivateEvent() {
this._tick = this.update.bind(this);
event.on(event.GAME_UPDATE, this._tick);
input.registerPointerEvent("pointerdown", this, this._onDown);
}
onDeactivateEvent() {
event.off(event.GAME_UPDATE, this._tick);
input.releasePointerEvent("pointerdown", this);
}
Pool-recycled objects never fire onDestroyEvent on removal β removeChildNow
first tries to return the child to the pool that built it, and only calls
destroy() when nothing owns it. So anything taken from a createPool goes
back to that pool instead. Anything you clean up there will not run. destroy() is also what calls
releaseAllPointerEvents(this), which is why a
FAQ
Why does event.off not work?
The skill says removal matches both the handler and its context, so the same pair must be given.
Why do listeners pile up?
An inline arrow function or a new bind call cannot be removed later, so the skill says to store the reference.
Can I create my own events?
Yes. Custom event names work at runtime, though the typed list covers the engine's own events.
Details
Original description by the author
Use this skill for the melonJS event bus and lifecycle hooks β subscribing to engine events, the per-frame and per-state event names, renderable lifecycle callbacks, and how to unsubscribe without leaking. Triggers on: event.on, event.off, event.once, event.emit, GAME_UPDATE, GAME_BEFORE_DRAW, LEVEL_LOADED, LOADER_COMPLETE, STATE_CHANGE, CANVAS_ONRESIZE, POINTERMOVE, WORLD_STEP, onActivateEvent, onDeactivateEvent, onDestroyEvent, lifecycle, subscribe, listener.
repo: melonjs/melonJS
Popularity
- GitHub stars
- 6.4k
- Ranking position
- #14202
- License
- MIT
- Updated
- 11/2/2026
Comments
- β¦
More skills from this repository
melonjs-20-migration
6.4kHelps your agent write melonJS game code for version 20 and avoid outdated methods from older versions, such as the required async startup.
melonjs-3d-assets
6.4kHelps your agent load 3D models such as glTF, GLB and OBJ files into a melonJS browser game, with scale and lighting options.
melonjs-3d
6.4kHelps your agent build 3D or 2.5D scenes in a melonJS browser game, covering cameras, meshes, lights and depth, using the engine's own axis rules.
melonjs-audio
6.4kHelps your agent add sound effects and music to a melonJS game, including audio setup, background tracks, volume control and spatial sound.
Similar skills
diffs
394kLets your agent produce a real, shareable diff of edits as a viewer link or image file, instead of a hand-written summary.
coding-agent
394kHelps your agent hand a larger coding job to Codex, Claude Code, or OpenCode as a background worker, then check on its progress.
gh-issues
394kHelps your agent find GitHub issues, pick the ones to work on, and open pull requests with fixes, including follow-up on review comments.