HTML and CSS Reference
In-Depth Information
Bootstrapping the Input Module
The main goal of the input module is to bind some sort of input action, whether a keypress, an onscreen keypad
press, or a joypad movement to a specific action, such as left or right . It shouldn't matter how the action
gets triggered; the game should see the same stream of input coming in.
The game receives input in two ways. The first is by looking at the Q.inputs object:
Q.inputs['fire'] // true if fire is being held down
Each bound action has an object key set to true if that key is currently held down. This is useful for move-
ment in which you want to check each frame to see if the user is moving and, if so, update their position appro-
For actions triggered on a specific press rather than just holding down the button, the input module inherits
from Evented to allow the binding of listeners. For example:
Q.input.bind('fire',function() {
console.log("Fire pressed");
Q.input.bind('fireUp',function() {
console.log("Fire released");
Supporting both methods can help keep your game step code from needing to poll every input each frame to
check for changes.
Each of the input methods will be configurable with what events are triggered, but some defaults can handle
the most common cases to prevent the need for too much configuration.
To start with the module, open a new file called quintus_input.js in the same directory as quintus.js
and add the code in Listing 10-3 .
Listing 10-3: The base Quintus.Input module
Quintus.Input = function(Q) {
var KEY_NAMES = { LEFT: 37, RIGHT: 39, SPACE: 32,
UP: 38, DOWN: 40,
Z: 90, X: 88
var DEFAULT_KEYS = { LEFT: 'left', RIGHT: 'right',
UP: 'up', DOWN: 'down',
SPACE: 'fire',
Z: 'fire',
X: 'action' };
var DEFAULT_TOUCH_CONTROLS = [ ['left','<' ],
['right','>' ],
['fire', 'a' ]];
Search WWH ::

Custom Search