Game Development Reference
In-Depth Information
Now in your game, you can simply query the controller for its state. Here is an example of
how I do this in one of my own games. I use this in the update loop of my game to make sure
I capture the controller events before anything else is updated in the game.
// Joystick controls
if (typeof(controller) != "undefined") {
var state = controller.getState();
if (state.connected) {
if (state.dpad_right)
this.player.rightDown();
else {
this.player.rightReleased();
}
if (state.dpad_left)
this.player.leftDown();
else {
this.player.leftReleased();
}
if (state.dpad_up || state.a)
this.player.jumpDown();
if (state.start)
this.togglePause();
}
} else {
//console.log("no controller");
}
Once you have this basic logic set up in your own game, you shouldn't have any issues ac-
cessing the other buttons on the game controller, such as the analog sticks, triggers, and even
the back and start buttons.
Search WWH ::




Custom Search