Arrays and Data objects

 

 

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

Data Objects / Associative arrays

Another type of data structure are the data objects: they store values associated to labels (a string key). They are very useful to store different types of values together.
To create a data object you define it as an Object type. Then you add properties to it with dot syntax, those properties act as labels or string keys to organice pieces of information inside the array.

var
character1 : Object = new Object() ;
var
name : data type = make new ;
character1 . charType = "Heroine" ;
name . property = value ;
character1 . charLevel = 6 ;
name . property = value ;
character1 . charHealth = 0.8 ;
name . property = value ;


You can also create this data object variable this way:

var
character1 : Object = { charType = "Heroine" , charLevel = 6 , charHealth = 0.8 } ;
var
name : data type = { property = value , property = value , property = value } ;

Objects are dynamic:you can add new properties (of any data type) whenever you want. See that you don't need to declare variables inside an Object (for the properties), you just need to devlare the value as seen above.

Arrays of Data Objects

Arrays of data objects are used in most games: they are used to store sprites or movieclips and the data about them.