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/zh-cn/web/api/gamepadbutton/index.html | 85 ++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 files/zh-cn/web/api/gamepadbutton/index.html (limited to 'files/zh-cn/web/api/gamepadbutton/index.html') diff --git a/files/zh-cn/web/api/gamepadbutton/index.html b/files/zh-cn/web/api/gamepadbutton/index.html new file mode 100644 index 0000000000..733a39da57 --- /dev/null +++ b/files/zh-cn/web/api/gamepadbutton/index.html @@ -0,0 +1,85 @@ +--- +title: GamepadButton +slug: Web/API/GamepadButton +translation_of: Web/API/GamepadButton +--- +
{{APIRef("Gamepad API")}}
+ +

GamepadButton 接口定义了在一个手柄或其他控制器的唯一的一个按键,允许访问不同控制器设备可用类型的按钮的当前状态。

+ +

GamepadButton 对象是由 {{domxref("Gamepad")}} 接口的 buttons 属性返回的可查询任意值的数组返回的。

+ +
+

注:上述情况是在 Firefox Gecko 28 及以上的;Chrome 和较早版本的 Firefox 访问此属性时仍然会返回一个双精浮点值的数组。

+
+ +

属性

+ +
+
{{domxref("GamepadButton.value")}} {{readonlyInline}}
+
一个用来表示按钮当前状态的双精浮点值,比如说许多现代控制器都有的扳机键。其值被规范至范围 0.0 —1.0 之间,其中 0.0 表示按钮没有被按下,而 1.0 表示按钮被完全按下 (按到底)。
+
{{domxref("GamepadButton.pressed")}} {{readonlyInline}}
+
一个指示当前按钮是被按下 (true) 还是没有被按下 (false) 的布尔值。
+
+ +

示例

+ +

接下来的代码来自于我 (文作者) 的 Gamepad API 按钮示例 (你可以在线查看示例,并在 Github 上查找源代码。) 注意代码分支——在 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")}}Initial definition
+ +

浏览器兼容性

+ +

{{Compat("api.GamepadButton")}}

+ +

另请参阅

+ +

使用 Gamepad API

-- cgit v1.2.3-54-g00ecf