melonjs-input
它能做什么
它介绍如何将按键映射为具名动作、响应鼠标和触摸,以及在 melonJS 游戏中处理游戏手柄。适用于构建操作控制的游戏开发者。
适用场景
- ✓当开发者希望方向键与游戏手柄的移动方式相同时。
- ✓当开发者希望跳跃键或开火键每按一次只触发一次时。
- ✓当开发者的可点击对象对指针输入没有反应时。
技能链接
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-input/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请让方向键移动我的角色,并让空格键每按一次只跳跃一次。”
每周 5 个新技能,1 分钟读完。
工作原理
该技能告诉 AI 将按键绑定到动作名称,然后在更新循环中检查这些动作,并对一次性动作进行锁定,使其每次按下只触发一次。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Input: keyboard, pointer and gamepad
Keyboard goes through named actions
Do not poll key codes. Bind a key to an action string, then poll the action — this is what lets gamepad and virtual controls map onto the same handling.
import { input } from "melonjs";
input.bindKey(input.KEY.LEFT, "left");
input.bindKey(input.KEY.RIGHT, "right");
input.bindKey(input.KEY.X, "jump", true); // true = locked: one press per keydown
// in update()
if (input.isKeyPressed("left")) { /* … */ }
if (input.isKeyPressed("jump")) { /* fires once per press, not per frame */ }
The third argument to bindKey is lock (default false). Without it,
isKeyPressed stays true for as long as the key is held — correct for movement,
wrong for jump or fire. A locked action stays consumed until the key is released
or you call input.unlockKey("jump"). Use input.keyStatus(action) when you
want the raw held/not-held state regardless of locking.
A poll can miss a press entirely
isKeyPressed is sampled once per frame, and keyup decrements the key's
counter the moment it arrives — locked or not. A press that goes down and up
between two frames therefore leaves the counter back at zero and is never
seen. At 60fps that is anything under ~16ms: rare from a human finger, routine
from a synthetic event, and more likely the slower the frame rate.
For a screen where a missed press is a dead end — a splash, a "press any key", a notice to acknowledge — listen for the event instead, which cannot be missed:
import { event } from "melonjs";
// action is the bound action name, or undefined for an unbound key
const onKey = (action) => { if (action === "menuSelect") continue(); };
event.on(event.KEYDOWN, onKey); // in onResetEvent
event.off(event.KEYDOWN, onKey); // in onDestroyEvent — it does not self-remove
KEYDOWN fires for every key, bound or not, so matching on action is what
narrows it; it also repeats while the key is held, so guard anything that must
happen once. Keep polling for movement and for a menu cursor, where one step
per press and a key held across a state change are the things that matter.
The full signature is bindKey(keyCode, action, lock?, preventDefault?);
preventDefault defaults to input.preventDefault (itself true). Pass the
fourth argument per binding, or change the global default with
input.setPreventDefault(false) — assigning input.preventDefault = false
does not work, it is a read-only module binding.
Pointer events need isKinematic = false
This is the trap. Renderable.isKinematic defaults to true, and both the
broadphase and the pointer dispatcher skip any candidate whose isKinematic is
still true. The handler registers successfully and simply never fires.
class Button extends Renderable {
constructor(x, y, w, h) {
super(x, y, w, h);
this.isKinematic = false; // ← required
}
onActivateEvent() {
input.registerPointerEvent("pointerdown", this, this.onDown.bind(this));
}
onDeactivateEvent() {
input.releasePointerEvent("pointerdown", this);
}
}
Container, Camera2d, Draggable, DropTarget and the UI* elements set it
to false for you, and so does attaching a physics body (Body's constructor
sets ancestor.isKinematic = false) — so plain Renderable and Sprite
subclasses with no body are the ones that catch people.
Register in onActivateEvent and release in onDeactivateEvent, not in the
constructor — pooled objects are reused and would otherwise accumulate handlers.
app.viewport works as a whole-screen region when you want global clicks. Note
it is asked before everything else, not as a fallback after nothing matched:
the dispatcher appends it to the candidate list and walks that list from the
end. So a viewport handler must not return false unless it really means to
swallow every pointer event in the game.
The accepted event names are exactly "pointerdown", "pointerup",
"pointermove", "pointercancel", "pointerenter", "pointerover",
"pointerleave" and "wheel". Anything else — "click", "mousedown",
"touchstart" — makes both registerPointerEvent and releasePointerEvent
throw invalid event type. melonJS maps the canonical name onto whichever
mouse/touch events the device actually supports, so you always register the
pointer name.
Which object gets the event
The dispatcher collects the regions nea
常见问题
为什么我的跳跃会触发很多次?
如果不加锁定,按住的键每一帧都会被视为已按下,因此该技能建议对一次性动作进行锁定。
为什么指针事件无法到达我的对象?
该技能提到一个 kinematic 设置,它会悄无声息地阻止指针事件。
它支持游戏手柄吗?
支持。该技能说明游戏手柄可以映射到相同的动作名称上。
详情
作者的原始描述
Use this skill for keyboard, pointer, mouse, touch and gamepad input in melonJS. Covers the bindKey action indirection, registerPointerEvent on regions, the isKinematic requirement that silently blocks pointer events, which of several overlapping objects receives an event and how to consume it, world vs screen pointer coordinates, and gamepad mapping onto the same actions. Triggers on: input, bindKey, isKeyPressed, KEY, registerPointerEvent, releasePointerEvent, pointerdown, pointerup, pointermove, POINTERMOVE, gamepad, bindGamepad, triggerKeyEvent, virtual gamepad, touch controls, joystick, g
repo: melonjs/melonJS
热度
- GitHub 星标
- 6.4k
- 排名
- #14204
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
melonjs-20-migration
6.4k帮你的智能体按 melonJS 20 版本编写游戏代码,避免使用旧版本中已过时的方法,例如必需的异步启动。
melonjs-3d-assets
6.4k帮你的智能体将 glTF、GLB 和 OBJ 等 3D 模型加载到 melonJS 浏览器游戏中,并提供缩放和光照选项。
melonjs-3d
6.4k帮你的智能体在 melonJS 浏览器游戏中构建 3D 或 2.5D 场景,涵盖相机、网格、灯光和深度,并遵循引擎自身的坐标轴规则。
melonjs-audio
6.4k帮你的智能体为 melonJS 游戏添加音效和音乐,包括音频设置、背景音轨、音量控制和空间声音。