Game Development Reference
In-Depth Information
Finally, at the end of each game-loop iteration, the Mouse object is reset. The only thing you need to
do here is set the pressed variables to false again:
Mouse_Singleton.prototype.reset = function () {
this._left.pressed = false;
this._middle.pressed = false;
this._right.pressed = false;
};
By setting the pressed variables to false after each game-loop iteration, you make sure a mouse or
key press is handled only once.
Arrays
You can also redesign keyboard input handling now that you have this ButtonState class—but
before you do that, let's introduce another concept you need, called an array . An array is basically a
numbered list. Have a look at the following examples:
var emptyArray = [];
var intArray = [4, 8, 15, 16, 23, 42];
Here you see two declarations and initializations of array variables. The first declaration is an empty
array (no elements). The second variable, intArray , refers to an array of length 6. You can access the
elements in the array by their index, where the first element in the array has index 0:
var v = intArray[0]; // contains the value 4
var v2 = intArray[4]; // contains the value 23
You use square brackets to access elements in the array. You can also modify the values in the array
using the same square brackets:
intArray[1] = 13; // intArray now is [4, 13, 15, 16, 23, 42]
It's also possible to add an element to the array:
intArray.push(-3); // intArray now is [4, 13, 15, 16, 23, 42, -3]
Finally, each array has a length variable that you can access to retrieve the length:
var l = intArray.length; // contains the value 7
You can use arrays in combination with for loops to do interesting things. Here's an example:
for (var i = 0; i < intArray.length; i++) {
intArray[i] += 10;
}
 
Search WWH ::




Custom Search