最后编辑的!
在GameController API中,是否需要持续轮询navigator.getGamepads()?
我之所以这样问,是因为我对这个函数的单个调用返回了length = 0。
根据我的Mac的蓝牙系统首选项,我的Nimbus+游戏机是连接的。
既然如此,我应该使用isetInterval` `并等待长度> 0吗?
编辑从这里开始:
Macintosh和蒙特利操作系统12.4:
Safari (15.5) reports length = 0
Firefox (102.0b6) reports length = 0
Chrome (102.0.5005.115) reports length = 4,
but each in the array being = null发布于 2022-06-20 13:17:08
首先,您需要等待连接游戏垫,这通常需要通过按其中一个按钮“唤醒”游戏垫:
window.addEventListener('gamepadconnected', (event) => {
console.log('✅ A gamepad was connected:', event.gamepad);
});一旦连接到游戏垫,您就可以启动游戏循环:
const pollGamepad = () => {
// Always call `navigator.getGamepads()` inside of
// the game loop, not outside.
const gamepads = navigator.getGamepads();
for (const gamepad of gamepads) {
// Disregard empty slots.
if (!gamepad) {
continue;
}
// Process the gamepad state.
console.log(gamepad);
}
// Call yourself upon the next animation frame.
// (Typically this happens every 60 times per second.)
window.requestAnimationFrame(pollGamepad);
};
// Kick off the initial game loop iteration.
pollGamepad();当游戏垫断开连接时,您应该停止轮询,这将通过一个事件通知您:
window.addEventListener('gamepaddisconnected', (event) => {
console.log('❌ A gamepad was disconnected:', event.gamepad);
});https://stackoverflow.com/questions/72594949
复制相似问题