मुख्य सामग्री पर जाएँ
Skillpedia
सामान्य कोडिंग6.4k · #14202

melonjs-events

यह क्या करती है

यह बताती है कि फ़्रेम अपडेट और लेवल लोड जैसे गेम इवेंट कैसे सुनें और सुनना कैसे बंद करें। यह भरोसेमंद इवेंट हैंडलिंग चाहने वाले गेम डेवलपर्स के लिए है।

कब इस्तेमाल करें

  • ✓जब डेवलपर चाहे कि उसका कोड हर फ़्रेम पर या लेवल लोड होने पर चले।
  • ✓जब डेवलपर का गेम समय के साथ धीमा हो क्योंकि पुराने इवेंट लिसनर अब भी सक्रिय हैं।
  • ✓जब डेवलपर को अपने गेम लॉजिक के लिए कस्टम इवेंट चाहिए।

Skill का लिंक

https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-events/SKILL.md

इसे कॉपी करके अपने AI (Claude, ChatGPT या Cursor) में पेस्ट करें और निर्देशों का पालन करने को कहें।

कैसे इस्तेमाल करें

सबसे आसान तरीका: मैसेज कॉपी करके अपने AI में पेस्ट करें। वह skill पढ़कर निर्देशों का पालन करेगा।

पेस्ट करने के लिए तैयार मैसेज

इस skill से मेरी मदद करो: https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-events/SKILL.md इस लिंक की SKILL.md फ़ाइल पढ़ो और मेरे काम में उसके निर्देशों का पालन करो।
क्या आप चाहते हैं कि आपका AI ऐसी skills खुद ढूँढे?मुफ़्त Skillpedia MCP इंस्टॉल करें: बस एक लाइन, और हर काम से पहले यह 15,000+ skills में खोजता है।1 मिनट में इंस्टॉल करें →

AI से क्या माँगें: उदाहरण

“melonJS में लेवल लोड होने पर मेरा कोड चलाइए, और सुनिश्चित कीजिए कि मैं बाद में लिसनर हटा सकूँ।”

हर हफ़्ते 5 नई skills, 1 मिनट में पढ़ें।

यह कैसे काम करती है

यह स्किल AI को हर handler का संदर्भ रखने या context argument इस्तेमाल करने को कहती है, ताकि बाद में वही handler हटाया जा सके।

तकनीकी सामग्री (मूल 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 काम क्यों नहीं करता?

स्किल कहती है कि हटाना handler और उसके context दोनों से मेल खाता है, इसलिए वही जोड़ी देनी होगी।

लिसनर क्यों जमा होते जाते हैं?

Inline arrow function या नया 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
अपडेट की गई
2/11/2026

क्या यह skill काम की है?

0

लॉग इन करें वोट करने के लिए।

कमेंट

  • …

इस repository की और skills

melonjs-20-migration

6.4k

आपके एजेंट को melonJS वर्शन 20 के लिए गेम कोड लिखने में मदद करती है और पुराने वर्शन के आउटडेटेड तरीकों से बचाती है, जैसे ज़रूरी async स्टार्टअप।

कोडिंगसामान्य कोडिंग

melonjs-3d-assets

6.4k

आपके एजेंट को glTF, GLB और OBJ जैसे 3D मॉडल melonJS ब्राउज़र गेम में लोड करने में मदद करती है, scale और lighting विकल्पों के साथ।

कोडिंगसामान्य कोडिंग

melonjs-3d

6.4k

आपके एजेंट को melonJS ब्राउज़र गेम में 3D या 2.5D दृश्य बनाने में मदद करती है, जिसमें कैमरा, मेश, लाइट और गहराई शामिल हैं, इंजन के अपने axis नियमों के साथ।

कोडिंगसामान्य कोडिंग

melonjs-audio

6.4k

आपके एजेंट को melonJS गेम में साउंड इफ़ेक्ट और संगीत जोड़ने में मदद करती है, जिसमें ऑडियो सेटअप, बैकग्राउंड ट्रैक, वॉल्यूम नियंत्रण और स्थानिक ध्वनि शामिल हैं।

कोडिंगसामान्य कोडिंग

मिलती-जुलती skills