Add a few lines to your game for pairing the gamepad, getting its state, and adding event listeners. Refer to the sample games for more complete examples. Below is some code to get you started:
var pad = Gamepad.create();
pad.pair().then(function (controllerPeer) {
console.log('Connected to controller');
}).then(initControls).catch(function (e) {
console.trace(e.stack ? e.stack : e);
});
function initControls() {
window.requestAnimationFrame(function () {
// In your game loop check `pad.state`, or you can listen to events.
});
pad.on('buttonpress', function (key) {
// Some button pressed.
}).on('buttondown', function (key) {
// Some button pushed down.
}).on('buttonup', function (key) {
// Some button released.
}).on('buttonchange', function (key, isPressed) {
// Some button changed.
});
pad.on('buttonpress.select', function (key) {
// SELECT button pressed.
}).on('buttondown.select', function (key) {
// SELECT button pushed down.
}).on('buttonup.select', function (key) {
// SELECT button released.
}).on('buttonchange.select', function (key, isPressed) {
// SELECT button changed.
});
}
// Totally optional, but when the user stops playing your game,
// for example, you can call `destroyControls` to remove any event
// listeners you have set.
function destroyControls() {
// Remove event listener for a particular listener function.
pad.off('buttonpress', buttonpressHandler);
// Remove all event listeners for a particular event type.
pad.off('buttonpress');
}
Develop
Install Node dependencies:
npm install
This installs these production dependencies:
plink-server: a simple Node-based WebSocket server – used as a signalling server for WebRTC
plink: a simple client-side library for WebRTC data channels — used to do peer communication between a game and controllers
And these developer dependencies:
browserify: a tool for packaging Node-flavoured CommonJS modules for the browser — used to compile JS for development and production bundles
gulp: a streaming build system and task runner — used for such tasks as browserify compilation, code linting, distribution, and running a development server
galaxy.js-mobile-gamepad 🎮
A JavaScript library for controlling an HTML5 game using WebRTC (falling back to WebSockets).
Used in conjunction with galaxy.js.
Downloads
Client
Host
Use in your own game
On the static server, open
client.html(which will loadgamepad-client.min.js).In your game, insert this script:
Add a few lines to your game for pairing the gamepad, getting its state, and adding event listeners. Refer to the sample games for more complete examples. Below is some code to get you started:
Develop
Install Node dependencies:
This installs these production dependencies:
And these developer dependencies:
(Optional) Set up symlinks for updating GitHub pages:
(Optional) To use custom settings for your local setup, first over a settings file:
Any value specified in
src/js/settings_local.jswill override the defaults insrc/js/settings.js.To rebuild (compile and minify) the scripts while developing and serve the files from a local server:
In another terminal session, start up the signalling server (plink-server):
Load an example game.
Load the Nintendo™-inspired controller.
Distribution
To build the files for distribution:
Several files will be written to the
dist/directory, including the main application file (uncompressed and minified):Deploying controller to a production server
Install Node dependencies:
Deploy the
dist/on a server (the “static server” we’ll call it).Credits