--- title: GamepadButton slug: Web/API/GamepadButton tags: - API - Gamepad API - Games - NeedsBufferSpecLink - NeedsMarkupWork - Référence(2) translation_of: Web/API/GamepadButton ---
GamepadButton インタフェースはゲームパッドやその他のコントローラーの各ボタンを定義します。このインタフェースによって、コントローラー上で使用できるボタンの状態を確認することができます。
GamepadButtonオブジェクトは、{{domxref("Gamepad")}}インタフェースのbuttons プロパティから取得できます。
注釈: これはFirefox Gecko 28以降の場合の説明であり、Chrome と以前のFirefoxのバージョンでは、浮動小数点数の配列が返されます。
true、押されていない場合はfalseになります。次のコードは、Gamepad API ボタンのデモです。Chromeでは、{{domxref("Navigator.getGamepads")}}にはwebkitプレフィックスが必要であり、各ボタンの値は浮動小数点数の配列として格納されます。Firefoxでは、{{domxref("Navigator.getGamepads")}}にはプレフィックスは不要で、各ボタンの値は{{domxref("GamepadButton")}}オブジェクトの配列に格納されます。使用するプロパティはこのオブジェクトの{{domxref("GamepadButton.value")}}または{{domxref("GamepadButton.pressed")}}プロパティのどちらかであり、どちらを使用すればいいかはボタンの種類に依存します。この例ではどちらにも対応しています。
function gameLoop() {
if(navigator.webkitGetGamepads) {
var gp = navigator.webkitGetGamepads()[0];
if(gp.buttons[0] == 1) {
b--;
} else if(gp.buttons[1] == 1) {
a++;
} else if(gp.buttons[2] == 1) {
b++;
} else if(gp.buttons[3] == 1) {
a--;
}
} else {
var gp = navigator.getGamepads()[0];
if(gp.buttons[0].value > 0 || gp.buttons[0].pressed == true) {
b--;
} else if(gp.buttons[1].value > 0 || gp.buttons[1].pressed == true) {
a++;
} else if(gp.buttons[2].value > 0 || gp.buttons[2].pressed == true) {
b++;
} else if(gp.buttons[3].value > 0 || gp.buttons[3].pressed == true) {
a--;
}
}
ball.style.left = a*2 + "px";
ball.style.top = b*2 + "px";
var start = rAF(gameLoop);
};
| 仕様 | 状態 | コメント |
|---|---|---|
| {{SpecName("Gamepad", "#gamepadbutton-interface", "GamepadButton")}} | {{Spec2("Gamepad")}} | 初版 |
{{Compat("api.GamepadButton")}}