HTML and CSS Reference
In-Depth Information
for(var b=0, len=controller.buttons.length; b<len; b++) {
if(controller.buttons[b]) {
if(b == 1) {
The first line is just checking if a gamepad is connected. The second line loops through connected gamepads. On
the third line, you're assigning a given gamepad object to a variable that will be used later. The fourth line is another
loop, this time going through all of the available buttons in your controller. The fifth line verifies if the button you're
actually checking is pressed. The sixth line is checking if the button is the one from the right face of the controller;
that is, you're asking for B (XBox 360) or O (PS3) in particular. If it's the button you're looking for and it's being pressed,
then you're starting a new game.
Gamepad Differences
When looping through the buttons, remember that different controllers may have different key mappings, so the
button[12] can be a top d-pad on an XBox 360 controller but something totally different on another controller. There
are plenty of controllers available, and the Gamepad API specification is not officially released yet, so remember this
when you're implementing gamepad keys in your game.
Hooking Up to the Event
There could be even easier solution if you want to use only a few buttons in your game. You can, for example, hook up
your own function inside the buttonPressed one and check for the specific button press. Let's assume you will have
a GAME object with your game's logic.
var GAME = {};
You can define startNewGame function that will do just that—start a new game.
GAME.startNewGame = function(){
alert("New game started!");
Now you can modify the buttonPressed function and add those lines that will check for a specific button
being pressed.
if(button == 1) { // button[1] is B or O
This is how the function looks like after the addition:
buttonPressed: function(event, pressed) {
var button = event.button;
if(pressed) {
Search WWH ::

Custom Search