Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1109651 of Desktop gamepad controls

  • Revision slug: Games/Techniques/Control_mechanisms/Desktop_with_gamepad
  • Revision title: Desktop gamepad controls
  • Revision id: 1109651
  • Created:
  • Creator: chrisdavidmills
  • Is current revision? No
  • Comment

Revision Content

{{PreviousMenuNext("Games/Techniques/Control_mechanisms/Desktop_with_mouse_and_keyboard", "Games/Techniques/Control_mechanisms/Other", "Games/Techniques/Control_mechanisms")}}

Now we'll look at adding something extra — support for gamepad controls, via the Gamepad API. It brings a console-like experience to your web games.

API status, browser and hardware support

The Gamepad API is still in Working Draft status, although browser support is already quite good — around 63% global coverage, according to canuse.com. The list of supported devices is also quite extensive — most popular gamepads (e.g. XBox 360 or PS3) should be suitable for web implementations.

GamepadAPI object

Let's move on to the coding part — we'll add Gamepad API support to the Captain Rogers game we created with Phaser. This is pure JavaScript code however, so can be used in any other project no matter what framework was used.

First off, we'll create a small library that will take care of handling the input for us. Here's the GamepadAPI object, which contains useful variables and functions:

var GamepadAPI = {
    active: false,
    controller: {},
    connect: function(event) {},
    disconnect: function(event) {},
    update: function() {},
    buttons: {
        layout: [],
        cache: [],
        status: [],
        pressed: function(button, state) {}
    }
    axes: {
        status: []
    }
};

The controller variable stores the information about the connected gamepad, and there's an active boolean variable we can use to know if the controller is connected or not. The connect() and disconnect() functions are bound to the following events:

window.addEventListener("gamepadconnected", GamepadAPI.connect);
window.addEventListener("gamepaddisconnected", GamepadAPI.disconnect);

They are fired when the gamepad is connected and disconnected respectively. The next function is update(), which updates the information about the pressed buttons and axes.

The buttons variable contains the layout of a given controller (for example which buttons are where, because an XBox 360 layout may be different to a generic one), the cache containing the information about the buttons from the previous frame and the status containing the information from the current frame.

The pressed() function gets the input data and sets the information about it in our object, and the axes variable stores the table with the values.

After the gamepad is connected, the information about the controller is stored in the object:

connect: function(event) {
    GamepadAPI.controller = event.gamepad;
    GamepadAPI.active = true;
},

The disconnect function removes the information from the object:

disconnect: function(event) {
    delete GamepadAPI.controller;
    GamepadAPI.active = false;
},

The update() function is executed in the update loop of the game on every frame, so it contains the latest information on the pressed buttons:

update: function() {
  GamepadAPI.buttons.cache = [];
  for(var k=0; k<GamepadAPI.buttons.status.length; k++) {
    GamepadAPI.buttons.cache[k] = GamepadAPI.buttons.status[k];
  }
  GamepadAPI.buttons.status = [];
  var c = GamepadAPI.controller || {};
  var pressed = [];
  if(c.buttons) {
    for(var b=0,t=c.buttons.length; b<t; b++) {
      if(c.buttons[b].pressed) {
        pressed.push(GamepadAPI.buttons.layout[b]);
      }
    }
  }
  var axes = [];
  if(c.axes) {
    for(var a=0,x=c.axes.length; a<x; a++) {
      axes.push(c.axes[a].toFixed(2));
    }
  }
  GamepadAPI.axes.status = axes;
  GamepadAPI.buttons.status = pressed;
  return pressed;
},

The function above clears the buttons cache, and copies their status from the previous frame to the cache. Next, the button status is cleared and the new information is added. The same goes for the axes information — looping through axes adds the values to the array. Received values are assigned to the proper objects and returns the pressed info for debugging purposes.

The button.pressed() function detects the actual button presses and saves the information about them in the table.

pressed: function(button, hold) {
  var newPress = false;
  for(var i=0,s=GamepadAPI.buttons.status.length; i<s; i++) {
    if(GamepadAPI.buttons.status[i] == button) {
      newPress = true;
      if(!hold) {
        for(var j=0,p=GamepadAPI.buttons.cache.length; j<p; j++) {
          if(GamepadAPI.buttons.cache[j] == button) {
            newPress = false;
          }
        }
      }
    }
  }
  return newPress;
},

It loops through pressed buttons and if the button we're looking for is pressed, then the corresponding boolean variable is set to true. If we want to check the button is not held already (so it's a new press), then looping through the cached states from the previous frame does the job — if the button was already pressed, then we ignore the new press and set it to false.

Implementation

We now know what the GamepadAPI object looks like and what variables and functions it contain, so let's learn how all this is actually used in the game. To indicate that the gamepad controller is active we can show the user some custom text on the game's main menu screen.

The textGamepad object holds the text saying a gamepad has been connected, and is hidden by default. Here's the code we've prepared in the create() function that is executed once when the new state is created:

create() {
    // ...
    var message = 'Gamepad connected! Press Y for controls';
    var textGamepad = this.add.text(message, ...);
    textGamepad.visible = false;
}

In the update() function, which is executed every frame, we can wait until the controller is actually connected, so the proper text can be shown. Then we can keep the track of the information about pressed buttons by using the Gamepad.update() method, and react to the given information:

update: function() {
    // ...
    if(GamepadAPI.active) {
        if(!this.textGamepad.visible) {
            this.textGamepad.visible = true;
        }
        GamepadAPI.update();
        if(GamepadAPI.buttons.pressed('Start')) {
            // start the game
        }
        if(GamepadAPI.buttons.pressed('X')) {
            // turn on/off the sounds
        }
        if(GamepadAPI.buttons.pressed('Y','hold')) {
            if(!this.screenGamepadHelp.visible) {
                this.screenGamepadHelp.visible = true;
            }
        }
        else {
            if(this.screenGamepadHelp.visible) {
                this.screenGamepadHelp.visible = false;
            }
        }
    }
}

When pressing the Start button the relevant function will be called to begin the game, and the same approach is used for turning the audio on and off. There's an option wired up to show screenGamepadHelp, which holds an image with all the button controls explained — if the Y button is pressed and held, the help becomes visible; when it is released the help diappears.

---IMG_BUTTONS_EXPLAINED---

On-screen instructions

When the game is started, some introductory text is shown that shows you available controls — we are already detecting if the game is launched on desktop or mobile then showing a relevant message for the device, but we can go even further, to allow for the presence of a gamepad:

create() {
    // ...
    if(this.game.device.desktop) {
        if(GamepadAPI.active) {
            moveText = 'DPad or left Stick\nto move';
            shootText = 'A to shoot,\nY for controls';
        }
        else {
            moveText = 'Arrow keys\nor WASD to move';
            shootText = 'X or Space\nto shoot';
        }
    }
    else {
        moveText = 'Tap and hold to move';
        shootText = 'Tap to shoot';
    }
}

When on desktop, we can check if the controller is active and show the gamepad controls — if not, then the keyboard controls will be shown.

Gameplay controls

We can offer even more flexibility to the player by giving him main and alternative gamepad movement controls:

if(GamepadAPI.buttons.pressed('DPad-Up','hold')) {
    // move player up
}
else if(GamepadAPI.buttons.pressed('DPad-Down','hold')) {
    // move player down
}
if(GamepadAPI.buttons.pressed('DPad-Left','hold')) {
    // move player left
}
if(GamepadAPI.buttons.pressed('DPad-Right','hold')) {
    // move player right
}
if(GamepadAPI.axes.status && GamepadAPI.axes.status[0]) {
    if(GamepadAPI.axes.status[0] > 0.5) {
        // move player up
    }
    else if(GamepadAPI.axes.status[0] < -0.5) {
        // move player down
    }
    if(GamepadAPI.axes.status[1] > 0.5) {
        // move player left
    }
    else if(GamepadAPI.axes.status[1] < -0.5) {
        // move player right
    }
}

They can now move the ship on the screen by using the DPad buttons, or the left stick axes.

Have you noticed that the current value of the axes is evaluated against 0.5? It's because axes are having floating point values while buttons are booleans. After a certain threshold is reached we can assume the input is done deliberately by the user and can act accordingly.

For the shooting controls, we used the A button — when it is held down, a new bullet is spawned, and everything else is handled by the game:

if(GamepadAPI.buttons.pressed('A','hold')) {
    this.spawnBullet();
}

Showing the screen with all the controls looks exactly the same as in the main menu:

if(GamepadAPI.buttons.pressed('Y','hold')) {
    if(!this.screenGamepadHelp.visible) {
        this.screenGamepadHelp.visible = true;
    }
}
else {
    if(this.screenGamepadHelp.visible) {
        this.screenGamepadHelp.visible = false;
    }
}

If the B button is pressed, the game is paused:

if(gamepadAPI.buttonPressed('B')) {
    this.managePause();
}

Pause and game over states

We already learned how to control the whole lifecycle of the game: pausing the gameplay, restarting it, or getting back to the main menu. It works smooth on mobile and desktop, and adding gamepad controls is just as straightforward — in the update() function, we check to see if the current state status is paused — if so, the relevant actions are enabled:

if(GamepadAPI.buttons.pressed('Start')) {
    this.managePause();
}
if(GamepadAPI.buttons.pressed('Back')) {
    this.stateBack();
}

Similarly, when the gameover state status is active, then we can allow the user to restart the game instead of continuing it:

if(GamepadAPI.buttons.pressed('Start')) {
    this.stateRestart();
}
if(GamepadAPI.buttons.pressed('Back')) {
    this.stateBack();
}

When the game over screen is visible, the Start button restarts the game while the Back button helps us get back to the main menu. The same goes for when the game is paused: the Start button unpauses the game and the Back button goes back, just like before.

Summary

That's it! We have successfully implemented gamepad controls in our game — try connecting any popular controller like the XBox 360 one and see for yourself how fun it is to avoid the asteroids and shoot the aliens with a gamepad.

Now we can move on and explore new, even more unconventional ways to control the HTML5 game like waving your hand in front of the laptop or screaming into your microphone.

{{PreviousMenuNext("Games/Techniques/Control_mechanisms/Desktop_with_mouse_and_keyboard", "Games/Techniques/Control_mechanisms/Other", "Games/Techniques/Control_mechanisms")}}

 

Revision Source

<p>{{PreviousMenuNext("Games/Techniques/Control_mechanisms/Desktop_with_mouse_and_keyboard", "Games/Techniques/Control_mechanisms/Other", "Games/Techniques/Control_mechanisms")}}</p>

<p class="summary">Now we'll look at adding something extra — support for gamepad controls, via the Gamepad API. It brings a console-like experience to your web games.</p>

<h2 id="API_status_browser_and_hardware_support">API status, browser and hardware support</h2>

<p>The <a href="/en-US/docs/Web/API/Gamepad_API">Gamepad API</a> is still in Working Draft status, although browser support is already quite good — around 63% global coverage, according to <a href="https://caniuse.com/#search=gamepad">canuse.com</a>. The list of supported devices is also quite extensive — most popular gamepads (e.g. XBox 360 or PS3) should be suitable for web implementations.</p>

<h2 id="GamepadAPI_object">GamepadAPI object</h2>

<p>Let's move on to the coding part — we'll add Gamepad API support to the <a class="external external-icon" href="https://rogers2.enclavegames.com/demo/">Captain Rogers</a> game we created with Phaser. This is pure JavaScript code however, so can be used in any other project no matter what framework was used.</p>

<p>First off, we'll create a small library that will take care of handling the input for us. Here's the <code>GamepadAPI</code> object, which contains useful variables and functions:</p>

<pre class="brush: js">
var GamepadAPI = {
    active: false,
    controller: {},
    connect: function(event) {},
    disconnect: function(event) {},
    update: function() {},
    buttons: {
        layout: [],
        cache: [],
        status: [],
        pressed: function(button, state) {}
    }
    axes: {
        status: []
    }
};</pre>

<p>The <code>controller</code> variable stores the information about the connected gamepad, and there's an <code>active</code> boolean variable we can use to know if the controller is connected or not. The <code>connect()</code> and <code>disconnect()</code> functions are bound to the following events:</p>

<pre class="brush: js">
window.addEventListener("gamepadconnected", GamepadAPI.connect);
window.addEventListener("gamepaddisconnected", GamepadAPI.disconnect);</pre>

<p>They are fired when the gamepad is connected and disconnected respectively. The next function is <code>update()</code>, which updates the information about the pressed buttons and axes.</p>

<p>The <code>buttons</code> variable contains the <code>layout</code> of a given controller (for example which buttons are where, because an XBox 360 layout may be different to a generic one), the <code>cache</code> containing the information about the buttons from the previous frame and the <code>status</code> containing the information from the current frame.</p>

<p>The <code>pressed()</code> function gets the input data and sets the information about it in our object, and the <code>axes</code> variable stores the table with the values.</p>

<p>After the gamepad is connected, the information about the controller is stored in the object:</p>

<pre class="brush: js">
connect: function(event) {
    GamepadAPI.controller = event.gamepad;
    GamepadAPI.active = true;
},</pre>

<p>The <code>disconnect</code> function removes the information from the object:</p>

<pre class="brush: js">
disconnect: function(event) {
    delete GamepadAPI.controller;
    GamepadAPI.active = false;
},</pre>

<p>The <code>update()</code> function is executed in the update loop of the game on every frame, so it contains the latest information on the pressed buttons:</p>

<pre class="brush: js">
update: function() {
  GamepadAPI.buttons.cache = [];
  for(var k=0; k&lt;GamepadAPI.buttons.status.length; k++) {
    GamepadAPI.buttons.cache[k] = GamepadAPI.buttons.status[k];
  }
  GamepadAPI.buttons.status = [];
  var c = GamepadAPI.controller || {};
  var pressed = [];
  if(c.buttons) {
    for(var b=0,t=c.buttons.length; b&lt;t; b++) {
      if(c.buttons[b].pressed) {
        pressed.push(GamepadAPI.buttons.layout[b]);
      }
    }
  }
  var axes = [];
  if(c.axes) {
    for(var a=0,x=c.axes.length; a&lt;x; a++) {
      axes.push(c.axes[a].toFixed(2));
    }
  }
  GamepadAPI.axes.status = axes;
  GamepadAPI.buttons.status = pressed;
  return pressed;
},</pre>

<p>The function above clears the buttons cache, and copies their status from the previous frame to the cache. Next, the button status is cleared and the new information is added. The same goes for the axes information — looping through axes adds the values to the array. Received values are assigned to the proper objects and returns the pressed info for debugging purposes.</p>

<p>The <code>button.pressed()</code> function detects the actual button presses and saves the information about them in the table.</p>

<pre class="brush: js">
pressed: function(button, hold) {
  var newPress = false;
  for(var i=0,s=GamepadAPI.buttons.status.length; i&lt;s; i++) {
    if(GamepadAPI.buttons.status[i] == button) {
      newPress = true;
      if(!hold) {
        for(var j=0,p=GamepadAPI.buttons.cache.length; j&lt;p; j++) {
          if(GamepadAPI.buttons.cache[j] == button) {
            newPress = false;
          }
        }
      }
    }
  }
  return newPress;
},</pre>

<p>It loops through pressed buttons and if the button we're looking for is pressed, then the corresponding boolean variable is set to <code>true</code>. If we want to check the button is not held already (so it's a new press), then looping through the cached states from the previous frame does the job — if the button was already pressed, then we ignore the new press and set it to <code>false</code>.</p>

<h2 id="Implementation">Implementation</h2>

<p>We now know what the <code>GamepadAPI</code> object looks like and what variables and functions it contain, so let's learn how all this is actually used in the game. To indicate that the gamepad controller is active we can show the user some custom text on the game's main menu screen.</p>

<p>The <code>textGamepad</code> object holds the text saying a gamepad has been connected, and is hidden by default. Here's the code we've prepared in the <code>create()</code> function that is executed once when the new state is created:</p>

<pre class="brush: js">
create() {
    // ...
    var message = 'Gamepad connected! Press Y for controls';
    var textGamepad = this.add.text(message, ...);
    textGamepad.visible = false;
}</pre>

<p>In the <code>update()</code> function, which is executed every frame, we can wait until the controller is actually connected, so the proper text can be shown. Then we can keep the track of the information about pressed buttons by using the <code>Gamepad.update()</code> method, and react to the given information:</p>

<pre class="brush: js">
update: function() {
    // ...
    if(GamepadAPI.active) {
        if(!this.textGamepad.visible) {
            this.textGamepad.visible = true;
        }
        GamepadAPI.update();
        if(GamepadAPI.buttons.pressed('Start')) {
            // start the game
        }
        if(GamepadAPI.buttons.pressed('X')) {
            // turn on/off the sounds
        }
        if(GamepadAPI.buttons.pressed('Y','hold')) {
            if(!this.screenGamepadHelp.visible) {
                this.screenGamepadHelp.visible = true;
            }
        }
        else {
            if(this.screenGamepadHelp.visible) {
                this.screenGamepadHelp.visible = false;
            }
        }
    }
}</pre>

<p>When pressing the <code>Start</code> button the relevant function will be called to begin the game, and the same approach is used for turning the audio on and off. There's an option wired up to show <code>screenGamepadHelp</code>, which holds an image with all the button controls explained — if the <code>Y</code> button is pressed and held, the help becomes visible; when it is released the help diappears.</p>

<p>---IMG_BUTTONS_EXPLAINED---</p>

<h2 id="On-screen_instructions">On-screen instructions</h2>

<p>When the game is started, some introductory text is shown that shows you available controls — we are already detecting if the game is launched on desktop or mobile then showing a relevant message for the device, but we can go even further, to allow for the presence of a gamepad:</p>

<pre class="brush: js">
create() {
    // ...
    if(this.game.device.desktop) {
        if(GamepadAPI.active) {
            moveText = 'DPad or left Stick\nto move';
            shootText = 'A to shoot,\nY for controls';
        }
        else {
            moveText = 'Arrow keys\nor WASD to move';
            shootText = 'X or Space\nto shoot';
        }
    }
    else {
        moveText = 'Tap and hold to move';
        shootText = 'Tap to shoot';
    }
}</pre>

<p>When on desktop, we can check if the controller is active and show the gamepad controls — if not, then the keyboard controls will be shown.</p>

<h2 id="Gameplay_controls">Gameplay controls</h2>

<p>We can offer even more flexibility to the player by giving him main and alternative gamepad movement controls:</p>

<pre class="brush: js">
if(GamepadAPI.buttons.pressed('DPad-Up','hold')) {
    // move player up
}
else if(GamepadAPI.buttons.pressed('DPad-Down','hold')) {
    // move player down
}
if(GamepadAPI.buttons.pressed('DPad-Left','hold')) {
    // move player left
}
if(GamepadAPI.buttons.pressed('DPad-Right','hold')) {
    // move player right
}
if(GamepadAPI.axes.status &amp;&amp; GamepadAPI.axes.status[0]) {
    if(GamepadAPI.axes.status[0] &gt; 0.5) {
        // move player up
    }
    else if(GamepadAPI.axes.status[0] &lt; -0.5) {
        // move player down
    }
    if(GamepadAPI.axes.status[1] &gt; 0.5) {
        // move player left
    }
    else if(GamepadAPI.axes.status[1] &lt; -0.5) {
        // move player right
    }
}</pre>

<p>They can now move the ship on the screen by using the <code>DPad</code> buttons, or the left stick axes.</p>

<p>Have you noticed that the current value of the axes is evaluated against <code>0.5</code>? It's because axes are having floating point values while buttons are booleans. After a certain threshold is reached we can assume the input is done deliberately by the user and can act accordingly.</p>

<p>For the shooting controls, we used the <code>A</code> button — when it is held down, a new bullet is spawned, and everything else is handled by the game:</p>

<pre class="brush: js">
if(GamepadAPI.buttons.pressed('A','hold')) {
    this.spawnBullet();
}</pre>

<p>Showing the screen with all the controls looks exactly the same as in the main menu:</p>

<pre class="brush: js">
if(GamepadAPI.buttons.pressed('Y','hold')) {
    if(!this.screenGamepadHelp.visible) {
        this.screenGamepadHelp.visible = true;
    }
}
else {
    if(this.screenGamepadHelp.visible) {
        this.screenGamepadHelp.visible = false;
    }
}</pre>

<p>If the <code>B</code> button is pressed, the game is paused:</p>

<pre class="brush: js">
if(gamepadAPI.buttonPressed('B')) {
    this.managePause();
}</pre>

<h2 id="Pause_and_game_over_states">Pause and game over states</h2>

<p>We already learned how to control the whole lifecycle of the game: pausing the gameplay, restarting it, or getting back to the main menu. It works smooth on mobile and desktop, and adding gamepad controls is just as straightforward — in the <code>update()</code> function, we check to see if the current state status is <code>paused</code> — if so, the relevant actions are enabled:</p>

<pre class="brush: js">
if(GamepadAPI.buttons.pressed('Start')) {
    this.managePause();
}
if(GamepadAPI.buttons.pressed('Back')) {
    this.stateBack();
}</pre>

<p>Similarly, when the <code>gameover</code> state status is active, then we can allow the user to restart the game instead of continuing it:</p>

<pre class="brush: js">
if(GamepadAPI.buttons.pressed('Start')) {
    this.stateRestart();
}
if(GamepadAPI.buttons.pressed('Back')) {
    this.stateBack();
}</pre>

<p>When the game over screen is visible, the <code>Start</code> button restarts the game while the <code>Back</code> button helps us get back to the main menu. The same goes for when the game is paused: the <code>Start</code> button unpauses the game and the <code>Back</code> button goes back, just like before.</p>

<h2>Summary</h2>

<p>That's it! We have successfully implemented gamepad controls in our game — try connecting any popular controller like the XBox 360 one and see for yourself how fun it is to avoid the asteroids and shoot the aliens with a gamepad.</p>

<p>Now we can move on and explore new, even more unconventional ways to control the HTML5 game like waving your hand in front of the laptop or screaming into your microphone.</p>

<p>{{PreviousMenuNext("Games/Techniques/Control_mechanisms/Desktop_with_mouse_and_keyboard", "Games/Techniques/Control_mechanisms/Other", "Games/Techniques/Control_mechanisms")}}</p>

<p>&nbsp;</p>
Revert to this revision