Data structures: Simple Arrays or Indexed arrays
Data structures are ways of storing information. This is specially important for games, since you have to store a lot of information about the game and the player's moves and choices.
The simplest data structure is an Array: it stores information as a list of values. A numeric index is used to identify individual elements. The first value holds the number 0.
var |
characterTypes | : | Array | = | new Array() | ; |
var |
name | : | data type | = | make new | ; |
characterTypes | = | [ | "Hero" | , | "Heroine" | , |
"Wizard" | , | "Shaman" | ] | ; |
Array variable |
= | [ | "value0" | , | "value1" | , | "value2" | , | "value3" | ] | ; |
In this example we used an Array of Strings but remember that arrays can hold any sort of value, such as numbers or display objects, like sprites, buttons, movieclips or videos. They can also mix different types of objects.
Having your game pieces organized in an Array has many advantages: you can easily loop through them to check each one for match or collision.
Array access operator []: A pair of square brackets surrounding an index or key that uniquely identifies an array element. This syntax is used after an array variable name to specify a single element of the array rather than the entire array.
characterTypes | [0] | refering to Hero | ||
characterTypes | [1] | refering to Heroine | ||
characterTypes | [2] | refering to Wizard |