From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/ja/web/api/gamepad/buttons/index.html | 98 +++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 files/ja/web/api/gamepad/buttons/index.html (limited to 'files/ja/web/api/gamepad/buttons/index.html') diff --git a/files/ja/web/api/gamepad/buttons/index.html b/files/ja/web/api/gamepad/buttons/index.html new file mode 100644 index 0000000000..d9f847ba44 --- /dev/null +++ b/files/ja/web/api/gamepad/buttons/index.html @@ -0,0 +1,98 @@ +--- +title: Gamepad.buttons +slug: Web/API/Gamepad/buttons +tags: + - API + - Gamepad API + - Games + - NeedsBetterSpecLink + - NeedsMarkupWork + - Property + - Reference + - Référence(2) +translation_of: Web/API/Gamepad/buttons +--- +

{{APIRef("Gamepad API")}}

+ +

{{domxref("Gamepad") }} インターフェースの Gamepad.buttons プロパティはデバイス上に存在するボタンを表すオブジェクトの配列を返します。

+ +

配列内の各エントリは、ボタンが押されていない場合は 0、ボタンが押されている場合は 0 以外の値 (通常は 1.0) です。各 {{{domxref("gamepadButton")}} オブジェクトには、pressed と value という 2 つのプロパティがあります :

+ + + +

構文

+ +
readonly    attribute GamepadButton[]     buttons;
+ +

+ +

下記のコードは Gamepad API ボタンのデモから取得したものです。(デモのライブを見ることができ、Githubでソースコードを見つけることができます)。コードをフォークする際には以降に気をつけてください — Chrome では {{domxref("Navigator.getGamepads")}} は webkit プレフィックスが必要で、ボタンの値は double 値の配列として格納されますが、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);
+};
+ +

+ +

{{domxref("gamepadButton")}} オブジェクトの配列。

+ +

仕様

+ + + + + + + + + + + + + + +
仕様状態コメント
{{SpecName("Gamepad", "#widl-Gamepad-buttons", "Gamepad.buttons")}}{{Spec2("Gamepad")}}初版
+ +

ブラウザの互換性

+ + + +

{{Compat("api.Gamepad.buttons")}}

+ +
 
+ +

参照

+ +

Gamepad API を利用する

-- cgit v1.2.3-54-g00ecf