melonjs-renderables
Skill này làm gì
Skill giải thích cách tạo loại đối tượng trò chơi riêng trong melonJS, tự vẽ chúng và làm cho chúng phản ứng với chuột hoặc cảm ứng. Dành cho lập trình viên xây dựng sprite và đối tượng tùy chỉnh.
Khi nào nên dùng
- ✓Khi lập trình viên viết một đối tượng tùy chỉnh và nó được vẽ sai vị trí khi di chuyển.
- ✓Khi đối tượng trò chơi có thể nhấp của lập trình viên không phản hồi khi nhấp.
- ✓Khi lập trình viên cần kiểm soát đối tượng nào hiện phía trước đối tượng khác.
Liên kết skill
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-renderables/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Đối tượng tùy chỉnh của tôi được vẽ ở góc trên bên trái thay vì nơi tôi di chuyển nó. Làm sao để sửa?”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Skill giải thích rằng việc vẽ tùy chỉnh phải dùng chính vị trí của đối tượng, và điểm neo phải đặt ở góc trên bên trái với đối tượng vẽ bằng tay. Skill cũng đề cập đến nhấp chuột và thứ tự vẽ.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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
Câu hỏi thường gặp
Vì sao đối tượng tùy chỉnh của tôi dính vào góc?
Skill nói một phương thức vẽ tùy chỉnh phải dùng chính giá trị vị trí của đối tượng.
Vì sao nhấp chuột của tôi không được ghi nhận?
Skill nhắc đến các quy tắc đầu vào có thể âm thầm chặn sự kiện con trỏ trên một số đối tượng.
Các lỗi này có hiển thị thông báo không?
Skill nói các vấn đề này lỗi một cách âm thầm, nên mã vẫn chạy mà không có thông báo lỗi nào.
Chi tiết
Mô tả gốc của tác giả
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
Độ phổ biến
- Sao GitHub
- 6.4k
- Vị trí xếp hạng
- #14212
- Giấy phép
- MIT
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
melonjs-20-migration
6.4kGiúp agent của bạn viết mã trò chơi melonJS cho phiên bản 20 và tránh các phương thức cũ, như bước khởi động bất đồng bộ bắt buộc.
melonjs-3d-assets
6.4kGiúp agent của bạn tải mô hình 3D như file glTF, GLB và OBJ vào trò chơi trình duyệt melonJS, kèm tùy chọn tỷ lệ và ánh sáng.
melonjs-3d
6.4kGiúp agent của bạn xây dựng cảnh 3D hoặc 2.5D trong trò chơi trình duyệt melonJS, gồm camera, lưới, đèn và độ sâu, theo quy tắc trục của công cụ.
melonjs-audio
6.4kGiúp agent của bạn thêm hiệu ứng âm thanh và nhạc nền vào trò chơi melonJS, gồm thiết lập âm thanh, nhạc nền, điều chỉnh âm lượng và âm thanh không gian.
Skill tương tự
diffs
394kGiúp agent của bạn tạo một diff có thể chia sẻ về các chỉnh sửa, dưới dạng liên kết xem hoặc file ảnh, thay vì một bản tóm tắt viết tay.
coding-agent
394kGiúp agent của bạn giao một việc lập trình lớn cho Codex, Claude Code hoặc OpenCode chạy nền, rồi theo dõi tiến độ của nó.
gh-issues
394kGiúp agent của bạn tìm issue trên GitHub, chọn những issue cần xử lý và mở pull request với bản sửa, kể cả theo dõi nhận xét đánh giá.