| name | handling-user-input |
| description | Game will always use the same control scheme/buttons and mapping of the gamepad and keyboard. Use when creating or modifying a game. |
Input Mechanism
When creating or modifying a game:
Gamepad
create() {
this.initGamePad();
}
initGamePad() {
this.input.gamepad.start();
this.activeGamepad = null;
if (this.input.gamepad.total > 0) {
this.activeGamepad = this.input.gamepad.gamepads.find(pad => pad !== null);
if (this.activeGamepad) {
console.log(`Found pre-connected gamepad: ${this.activeGamepad.id}`);
}
}
this.input.gamepad.on('connected', (pad) => {
if (!this.activeGamepad) {
this.activeGamepad = pad;
console.log(`Gamepad plugged in mid-game: ${pad.id}`);
}
});
}
update() {
const rawPad = this.activeGamepad;
const pad = rawPad ? {
...rawPad,
A: rawPad.B,
B: rawPad.A,
X: rawPad.Y,
Y: rawPad.X,
isButtonDown: (index) => {
if (index === 0) return rawPad.isButtonDown(1);
if (index === 1) return rawPad.isButtonDown(0);
if (index === 2) return rawPad.isButtonDown(3);
if (index === 3) return rawPad.isButtonDown(2);
return rawPad.isButtonDown(index);
},
left: rawPad.left,
right: rawPad.right,
up: rawPad.up,
down: rawPad.down,
axes: rawPad.axes,
buttons: rawPad.buttons
} : null;
}
Keyboard
Control tutorial / explainer
Update game config.json
The game's config.json, found in the game's root folder (e.g. <game-folder>/config.json) contains a controls section that defines the labels for buttons A, B, X, and Y. These arew the only 4 buttons allowed in the config.
When the game is created or modified, this config.json file should be updated to reflect the correct labels for each button. For example, if a code edit adds business logic so that the B enables the player to jump, set the label to "Jump". Labels must be short and limited to one word, or two words at most and MUST be an action (e.g. "Jump", "Run", "Fire", "Crawl", "Climb", etc.). There is limited space where the label will render in the UI so it must remain short.
If useful to explain the game's objective, the objective may be improved to include the new behavior.
Example:
{
"title": "Space Pilot",
"objective": "Navigate your spaceship while evading obstacles. Destroy them for points.",
"controls": {
"A": "Shoot",
"B": "Evade",
"X": "Roll Ship",
"Y": ""
}
}
Verification