What do I have to add to my JS driven game to interact With an external game joystick/controller?
What I have is a JS-driven game with buttons to control movement of a Paddle so it can “hit” a game piece.
I only have <button>
s to click. What do I have to add to my JS driven game to interact with an external game joystick/controller to click on.
I do nothing with mouseX
or mouseY
, just these clickable buttons.
The game piece bounces off 4 walls. It is the job of the user to steer the Paddle so it “hits” the game piece.
CodePudding user response:
I think you could use the html5 gamepad API if you're doing this in the browser. Just make sure to check out the browser compatibility section.
This blog post looks pretty good but here's a snippet from there.
// https://beej.us/blog/data/javascript-gamepad/
// Show button state
let gamepads = navigator.getGamepads();
// Blindly assuming this is connected
let gp = gamepads[0];
// Blindly assuming there's a button
let button = gp.buttons[0];
if (button.pressed) {
console.log("Button pressed!");
} else {
console.log("Button not pressed");
}
console.log("Button value: " button.value);