Game Development Reference
In-Depth Information
Here's an example of a bkn loop that displays the numbers from 0 to 4 in the Output panel.
bkn\$r]ne6ejp9,7e817e''%
w
pn]_a\$e%7
y
If you use this code in the class's constructor method, you'll see this in the Output panel when you
publish the SWF:
,
-
.
/
0
In the chapter's source files you'll find a file called bknHkkl*bh] in the BknHkkl folder. Open it in Flash
and choose Quick Project from the Project panel's drop-down menu. Double-click the I]ej[BknHkkl*
]o file to view the code. Test the project; you'll see the same result as shown previously.
It's easy to understand how a bkn loop works if you break down what it does into smaller parts. The
first thing it does is declare the variable that will be used to count the number of loops:
bkn\$ r]ne6ejp9, 7e817e''%
This creates a local integer variable called e, which is initialized to ,. The next statement tells the loop
how many times it should repeat:
bkn\$r]ne6ejp9,7 e81 7e''%
This is a conditional statement. It tells the loop to repeat “while the index variable is less than 5.” In
this example, the index variable is initialized to zero, so the loop will repeat until it reaches 4. You can
use any kind of conditional statement you want here.
The last statement increases the index variable by 1 each time the directives in the loop are run:
bkn\$r]ne6ejp9,7e817 e'' %
The first time the loop runs, e starts with its initialized value, which is zero. The next time it repeats, the
'' operator adds a value of 1. That means that e then equals - (because 0 plus 1 equals 1, of course).
The next time the loop repeats, 1 is added to e again, which results in a value of .. This repeats while
e is less than 5. As soon as it gets a value of 1, the loop stops dead in its tracks.
Although e'' is the most common way to increase the value of the index variable, you can use any
statement you like to increase or decrease it. For example, e'9. will increase the index variable by
2 each time the loop repeats. e)) will decrease it by 1 if you want your loop to count backward.