Game Development Reference
In-Depth Information
Next, the switch statement takes the value assigned to diceRoll and compares it against the
constant values of each case, in this example 1 through 6. When a matching case value is found, the
statement block that follows is executed.
The break statement ends the execution of the switch statement. After the final case , you'll see
a default statement that works similarly to the else statement—it is executed if none of the case
values match the argument originally passed to the switch statement.
Loops
Loops are useful for repeating lines of code. You might be checking the state of a group of game
objects, or creating new ones where you need to use identical lines of code for each item in the group.
The for Loop
To demonstrate a for loop, in MonoDevelop edit your code to the following:
#pragma strict
var targets : int = 4;
function Start () {
for(var i: int = 0; i < targets; i++)
{
Debug.Log("This is target #" + i);
}
}
Breaking down the for loop into parts:
(1) (2) (3) (4) (5) (6)
for(var i: int = 0; i < targets; i++)
(7)
{
(8)
Debug.Log(" This is target #" + i);
(9)
}
The keyword for .
1.
2.
Declaring the index variable as an integer and assigning an initial value of 0.
Computers start counting at 0 instead of 1, which can take a little getting
used to and is often mistaken as a bug when forgotten.
A semicolon separating the first and second components of the for
statement.
3.
 
Search WWH ::




Custom Search