melonjs-events
できること
このガイドは、フレームの更新やレベルの読み込みなど、ゲームのイベントを待ち受ける方法と、待ち受けを止める方法を説明します。信頼できるイベント処理が必要なゲーム開発者向けです。
使う場面
- ✓開発者が、毎フレームまたはレベルの読み込みが終わったときに実行されるコードを必要とするとき。
- ✓古いイベントのリスナーが残っているため、開発者のゲームが時間とともに遅くなるとき。
- ✓開発者が、自分のゲームロジック用のカスタムイベントを必要とするとき。
スキルのリンク
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-events/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“melonJSでレベルの読み込みが終わったときに私のコードを実行し、後でリスナーを削除できるようにしてください。”
毎週5つの新しいスキル。1分で読めます。
仕組み
このスキルはAIに、各ハンドラーへの参照を保持するか、コンテキストの引数を使うよう指示します。そうすることで、同じハンドラーを後で削除できます。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)を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
よくある質問
event.offが動かないのはなぜですか?
削除はハンドラーとそのコンテキストの両方に一致するため、同じ組み合わせを指定しなければならないとスキルは述べています。
リスナーが溜まるのはなぜですか?
インラインの矢印関数や新しいbindの呼び出しは後で削除できないため、参照を保存するようスキルは勧めています。
自分のイベントを作れますか?
はい。カスタムのイベント名は実行時に動きますが、型付きの一覧はエンジン独自のイベントを扱っています。
詳細
作者による元の説明
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
人気
- GitHubのスター
- 6.4k
- ランキング
- #14202
- ライセンス
- MIT
- 更新日
- 2026/11/2
コメント
- …
このリポジトリの他のスキル
melonjs-20-migration
6.4kエージェントが、バージョン20のmelonJSゲームのコードを書き、必要な非同期の起動など、古いバージョンの古いメソッドを避けられるよう支援します。
melonjs-3d-assets
6.4kエージェントが、glTF、GLB、OBJなどの3DモデルをmelonJSのブラウザゲームに読み込み、スケールとライティングのオプションを使えるよう支援します。
melonjs-3d
6.4kエージェントが、エンジン独自の軸のルールを使い、カメラ、メッシュ、ライト、奥行きを含めて、melonJSのブラウザゲームで3Dまたは2.5Dのシーンを作れるよう支援します。
melonjs-audio
6.4kエージェントが、音声の設定、背景の音楽、音量の制御、空間的な音響を含め、melonJSのゲームに効果音と音楽を追加できるよう支援します。
似ているスキル
diffs
394k編集内容の実際の共有可能な差分を、手書きの要約ではなく、ビューアーのリンクや画像ファイルとして作成するようエージェントを支援します。
coding-agent
394kCodex、Claude Code、OpenCodeに大きめのコーディング作業をバックグラウンドのワーカーとして渡し、その進捗を確認するようエージェントを支援します。
gh-issues
394kGitHubのイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。