melonjs-input
できること
このガイドは、melonJSのゲームでキーを名前のついたアクションに割り当て、マウスとタッチに応答し、ゲームパッドを扱う方法を説明します。操作を作るゲーム開発者向けです。
使う場面
- ✓開発者が、移動キーをゲームパッドと同じように動かしたいとき。
- ✓開発者が、ジャンプや発射のキーを1回の押下ごとに1度だけ発動させる必要があるとき。
- ✓開発者のクリックできるオブジェクトが、ポインターの入力に反応しないとき。
スキルのリンク
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-input/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“矢印キーでキャラクターを動かし、スペースバーは1回の押下ごとに1度だけジャンプするようにしてください。”
毎週5つの新しいスキル。1分で読めます。
仕組み
このスキルはAIに、キーをアクション名に割り当て、それらのアクションを更新ループで確認し、一度だけのアクションはロックして1回の押下ごとに1度だけ発動させるよう指示します。
技術的な内容(元の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エージェントが、バージョン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のイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。