【解決方法】ゲームパッド API: ボタン イベントが発生しない

プログラミングQA


I have a bluetooth controller in which I've tested all the numbers of the buttons with this Gamepad tester:<a href="https://gamepadtester.net/"> Gamepad Tester</a> so I'm pretty sure the values of the buttons are correct. However, nothing seems to fire, the Gamepad is shown to be "connected", just the button events not working. Below is the code:
JavaScript
function gameLoop() {
  if (navigator.webkitGetGamepads) {
    var wgp = navigator.webkitGetGamepads()[0];

    if (wgp.buttons[12] == 1 || wgp.buttons[4] == 1) {
      console.log('move');
    }
};

gameLoop();

Where could I have wrong? I'm using Chrome so I have the webkit prefix. Could the Gamepad tester be showing me the wrong buttons? Thanks for your help.

私が試したこと:

コードをデバッグしようとしましたが、役に立ちませんでした。

解決策 1

ゲームパッドがコレクションの最初として返されると想定しています。そうでない場合、コードは失敗するか、ボタンを選択しません。

まずは返されたコレクションの内容を見て、その中にゲームパッドがあるかどうかを確認します。

コメント

タイトルとURLをコピーしました