I'm getting this response but can't identify the problem – "Uncaught (in promise) TypeError: navigator.getGamepads is not a function"

huangapple go评论56阅读模式
英文:

I'm getting this response but can't identify the problem - "Uncaught (in promise) TypeError: navigator.getGamepads is not a function"

问题

我对JavaScript还不太了解,所以不太清楚该去哪里寻求帮助。这是一段代码,在开始时就出现了问题。
async function updateControls() {
    const gamepad = navigator.getGamepads()[0];

    if (gamepad != undefined) {

        gpConf.buttons.padUp = gamepad.buttons[12].value
        gpConf.buttons.padDown = gamepad.buttons[13].value
        gpConf.buttons.padLeft = gamepad.buttons[14].value
        gpConf.buttons.padRight = gamepad.buttons[15].value

        //setting axes
        gamepad.axes.forEach((value, index) => {
            gpControls.axes[index] = exponentiate(value)
        });

        //setting paddles
        gpControls.paddles.left = Number(gamepad.buttons[6].value.toFixed(3))
        gpControls.paddles.right = Number(gamepad.buttons[7].value.toFixed(3))
        gpControls.buttons.L1 = gamepad.buttons[4].value
        gpControls.buttons.R1 = gamepad.buttons[5].value
        gpControls.buttons.square = gamepad.buttons[2].value
        gpControls.buttons.triangle = gamepad.buttons[3].value

        setTimeout(() => {
            updateControls();
        }, 100);


        dispatch('car-control', gpControls)
        dispatch('car-conf', gpConf)
    }
}
我已经尝试过搜索帮助,但没有找到解决方法。出现的错误信息是:
"Uncaught (in promise) TypeError: navigator.getGamepads is not a function"
我不知道这是否相关,但我正在使用Windows 10和Firefox。
英文:

I'm new to JavaScript and so don't really know where to go.. Here is the code which falls over right at the start.

async function updateControls() {
    const gamepad = navigator.getGamepads()[0];

    if (gamepad != undefined) {

        gpConf.buttons.padUp = gamepad.buttons[12].value
        gpConf.buttons.padDown = gamepad.buttons[13].value
        gpConf.buttons.padLeft = gamepad.buttons[14].value
        gpConf.buttons.padRight = gamepad.buttons[15].value

        //setting axes
        gamepad.axes.forEach((value, index) => {
            gpControls.axes[index] = exponentiate(value)
        });

        //setting paddles
        gpControls.paddles.left = Number(gamepad.buttons[6].value.toFixed(3))
        gpControls.paddles.right = Number(gamepad.buttons[7].value.toFixed(3))
        gpControls.buttons.L1 = gamepad.buttons[4].value
        gpControls.buttons.R1 = gamepad.buttons[5].value
        gpControls.buttons.square = gamepad.buttons[2].value
        gpControls.buttons.triangle = gamepad.buttons[3].value

        setTimeout(() => {
            updateControls();
        }, 100);


        dispatch('car-control', gpControls)
        dispatch('car-conf', gpConf)
    }
}

I've googled for help but haven't found a resolution. the error that comes up is:
"Uncaught (in promise) TypeError: navigator.getGamepads is not a function"

I don't know if it is relevant but I'm using Windows 10 and Firefox.

答案1

得分: 1

根据此函数的MDN文档,Firefox需要一个安全上下文才能使用它。这意味着您需要从您自己的计算机上运行网页,或者通过HTTPS运行。您现在也可以使用其他浏览器,如Chrome或Edge,尽管它们将来可能也需要安全上下文。

英文:

According to the MDN docs on this function, Firefox requires a secure context to use it. That means you'll need to either be running the webpage from your own computer, or over HTTPS. You can also use other browsers such as Chrome or Edge for now, although they may require a secure context as well in the future.

huangapple
  • 本文由 发表于 2023年7月31日 21:17:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804034.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定