melonjs-renderables
できること
このガイドは、melonJSで独自のゲームオブジェクトの種類を作り、手で描画し、マウスやタッチに反応させる方法を説明します。カスタムのスプライトやオブジェクトを作るゲーム開発者向けです。
使う場面
- ✓開発者が、動かすと間違った位置に描画されるカスタムのオブジェクトを書いたとき。
- ✓開発者のクリックできるゲームオブジェクトが、クリックに反応しないとき。
- ✓開発者が、どのオブジェクトを他のオブジェクトの前に表示するかを制御する必要があるとき。
スキルのリンク
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-renderables/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“私のカスタムオブジェクトが、動かした位置ではなく左上の角に描画されます。どう直せばよいですか?”
毎週5つの新しいスキル。1分で読めます。
仕組み
このガイドは、カスタムの描画はオブジェクト自身の位置を使わなければならないこと、手で描くオブジェクトではアンカーを左上に設定する必要があることを説明します。クリックと描画の順序も扱います。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
Renderables, input and effects
Subclassing
Renderable, custom drawing, receiving pointer events, and applying post effects — with the traps that produce silently wrong output rather than an error.
Everything here is a silent failure: the code runs, nothing throws, and the result is wrong. Those are the cases worth memorising, because nothing in the type signatures warns you.
1. A custom draw() must position itself from this.pos
The renderer is not pre-translated to the renderable's position when draw()
is called. Container.draw translates by the container's own pos and then
calls child.preDraw / draw / postDraw; preDraw never translates by the
child's pos. Read this.pos and draw relative to it.
class Marker extends Renderable {
draw(renderer) {
// ✗ draws at the parent container's origin, not at the marker
renderer.fillRect(0, 0, this.width, this.height);
// ✓
renderer.fillRect(this.pos.x, this.pos.y, this.width, this.height);
}
}
The symptom is an object that renders correctly at position (0, 0) and appears stuck at the top-left as soon as you move it.
Zero the anchor in custom-draw renderables. preDraw does not translate to
pos, but it does apply the anchor offset (-width * anchorPoint.x, -height * anchorPoint.y). With the default centred anchor, drawing at
this.pos therefore lands half a size off:
constructor(x, y, w, h) {
super(x, y, w, h);
this.anchorPoint.set(0, 0); // ← or every custom draw is offset by -w/2, -h/2
}
2. isKinematic must be false to receive input or collisions
Renderable.isKinematic defaults to true, which means "physics collision
and input events will not impact this renderable". A kinematic renderable is
skipped by the pointer-event dispatcher entirely.
class Button extends Renderable {
constructor(x, y) {
super(x, y, 100, 40);
this.isKinematic = false; // ← required, or clicks never arrive
input.registerPointerEvent("pointerdown", this, () => { /* … */ });
}
}
The symptom is a handler that is registered, looks correct, and never fires.
Note that Container, Camera2d, Draggable, DropTarget and the UI*
elements set it to false for you, and so does the Body constructor on its
owner (which covers Entity and anything you give a body to) — so a plain
Renderable or Sprite subclass with no body is the case that catches people.
The same flag has a second, less obvious effect: it is also the opt-in to the
world broadphase. The quadtree/octree insert skips any child with
isKinematic === true, so body-less objects you want adapter.queryAABB /
querySphere to find need isKinematic = false as well. The builtin
narrowphase likewise requires both partners to be non-kinematic.
3. Draw order: only addChild(child, z) works
There are three separate traps here and they compound.
world.addChild(sprite, 10); // ✓ the only reliable form
renderable.zdoes not exist. There is nozaccessor anywhere onRenderable(nor onRect/Polygonabove it) — onlydepth, which proxiespos.z, and the container's defaultsortOn: "z"comparator readspos.z. Writingsprite.z = 100creates an inert property that nothing reads. Several shipped examples still containthis.z = Number.POSITIVE_INFINITY; those HUDs draw on top only because they happen to be added last. (Azkey in aSprite/ImageLayersettings object is a different thing and is real — it assignspos.zin the constructor — but trap 2 then overwrites it.)- Setting
depthbeforeaddChildis overwritten. WithautoDepthon (the default),addChildwithout an explicitzassignschild.pos.z = children.lengthafter the push, discarding whatever you set. - Setting
depthafteraddChilddoes not reorder. A"z"sort runs when a child is added (autoSort, deferred to the next tick), not whenpos.zchanges. If you must change it later, callcontainer.sort()yourself, or usemoveToTop/moveUp/moveDown/moveToBottom, which reorder the children themselves. (An"x"/"y"sort is different: see below.)
Sorting on "y" (or "x"), and sortOffset
A container with sortOn = "y" draws its children from the smallest pos.y to
the largest, so whatever is lower on screen covers what is above it (top-d
よくある質問
カスタムのオブジェクトが角に張り付くのはなぜですか?
カスタムの描画メソッドは、オブジェクトの位置の値そのものを使わなければならないとスキルは述べています。
クリックが反応しないのはなぜですか?
一部のオブジェクトでは、ポインターの入力を黙って遮断しうる入力のルールがあるとスキルは触れています。
これらのエラーは表示されますか?
スキルによると、これらの問題は黙って失敗するため、コードはエラーメッセージなしで動いてしまいます。
詳細
作者による元の説明
Use this skill when subclassing Renderable, writing a custom draw() method, handling pointer or click events on game objects, applying post effects and shaders, or controlling draw order. Covers the pos-relative draw contract, isKinematic and input, addPostEffect vs the deprecated shader property, and z-ordering. Triggers on: Tween, easing, animate, Renderable, extends Renderable, draw(, update(, isKinematic, pointerEvent, registerPointerEvent, onClick, addPostEffect, removePostEffect, ShaderEffect, addChild, z-order, anchorPoint, floating.
repo: melonjs/melonJS
人気
- GitHubのスター
- 6.4k
- ランキング
- #14212
- ライセンス
- 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のイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。