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 ++++++++++++++++++++++ .../zh-cn/web/api/gamepadbutton/pressed/index.html | 52 +++++++++++++ files/zh-cn/web/api/gamepadbutton/value/index.html | 53 ++++++++++++++ 3 files changed, 190 insertions(+) create mode 100644 files/zh-cn/web/api/gamepadbutton/index.html create mode 100644 files/zh-cn/web/api/gamepadbutton/pressed/index.html create mode 100644 files/zh-cn/web/api/gamepadbutton/value/index.html (limited to 'files/zh-cn/web/api/gamepadbutton') 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

diff --git a/files/zh-cn/web/api/gamepadbutton/pressed/index.html b/files/zh-cn/web/api/gamepadbutton/pressed/index.html new file mode 100644 index 0000000000..9126b26ddb --- /dev/null +++ b/files/zh-cn/web/api/gamepadbutton/pressed/index.html @@ -0,0 +1,52 @@ +--- +title: GamepadButton.pressed +slug: Web/API/GamepadButton/pressed +translation_of: Web/API/GamepadButton/pressed +--- +

{{APIRef("Gamepad API")}}

+ +

{{domxref("GamepadButton")}}接口下的 GamepadButton.pressed 属性返回一个表示按钮当然是被按下了 (true) 还是没有被按下 (false) 的布尔值。

+ +

语法

+ +
var isPressed = navigator.getGamepads()[0].pressed;
+
+ +

示例

+ +
var gp = navigator.getGamepads()[0]; // 获取第一个控制器对象
+
+if(gp.buttons[0].pressed == true) {
+  // 响应按钮按下
+}
+ +

+ +

一个 {{domxref("boolean")}} (布尔值)。

+ +

规范

+ + + + + + + + + + + + + + +
规范状态备注
{{SpecName("Gamepad", "#widl-GamepadButton-pressed", "GamepadButton.pressed")}}{{Spec2("Gamepad")}}Initial definition
+ +

浏览器兼容性

+ +

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

+ +

另请参阅

+ + diff --git a/files/zh-cn/web/api/gamepadbutton/value/index.html b/files/zh-cn/web/api/gamepadbutton/value/index.html new file mode 100644 index 0000000000..a7c83775a8 --- /dev/null +++ b/files/zh-cn/web/api/gamepadbutton/value/index.html @@ -0,0 +1,53 @@ +--- +title: GamepadButton.value +slug: Web/API/GamepadButton/value +translation_of: Web/API/GamepadButton/value +--- +

{{APIRef("Gamepad API")}}

+ +

{{domxref("GamepadButton")}}接口下的 GamepadButton.value 属性返回一个双精浮点值来表示许多现代控制器上的模拟按钮的状态,比如说扳机键。

+ +

其值被规范于范围 0.01.0 内, 0.0 表示按钮没有被按下,1.0 则表示按钮被完全按下。

+ +

语法

+ +
var pressState = navigator.getGamepads()[0].value;
+// 只读属性、双精浮点值
+
+ +

示例

+ +
var gp = navigator.getGamepads()[0];
+
+if(gp.buttons[0].value > 0) {
+  // 响应模拟按钮被按下
+} 
+ +

+ +

一个 {{domxref("double")}} (双精浮点值)。

+ +

规范

+ + + + + + + + + + + + + + +
规范状态备注
{{SpecName("Gamepad", "#widl-GamepadButton-value", "GamepadButton.value")}}{{Spec2("Gamepad")}}Initial definition
+ +

浏览器兼容性

+ +

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

+ +

另请参阅

+ +

使用 Gamepad API

-- cgit v1.2.3-54-g00ecf