Game Development Reference
In-Depth Information
You can execute all those instructions by calling the mainLoop function. The instructions that belong
to the function are grouped by using braces. Similar to grouping instructions, you can also group
variables into a bigger variable. That bigger variable then contains multiple values. Have a look at the
following example:
var gameCharacter = {
name : "Merlin",
skill : "Magician",
health : 100,
power : 230
};
This is an example of a composite variable . The variable gameCharacter consists of several values,
each of which has a name and a value that the name refers to. So, in a sense, the gameCharacter
variable is composed of other variables . You can see that, as in the body of a function, variables are
grouped between braces. Each subvariable has a name, and after the colon you specify the value that
this variable refers to. The expression consisting of the names and values enclosed by the braces is
called an object literal . Figure 3-6 shows the (partial) syntax diagram of an object literal expression.
object literal
{
name
:
expression
}
,
Figure 3-6. (Partial) syntax diagram of an object literal expression
After the declaration and initialization of the gameCharacter variable, the memory will look as
depicted in Figure 3-7 .
health
100
name
“Merlin”
gameCharacter
skill
“Magician”
power
230
Figure 3-7. Memory structure after creating a composite variable
You can access the data in a composite variable as follows:
gameCharacter.name = "Arjan";
var damage = gameCharacter.power * 10;
 
 
Search WWH ::




Custom Search