Game Development Reference
In-Depth Information
Nested Repeats
The body of a while instruction or a for instruction is an instruction. This instruction can be an
assignment, a method call, a block of instructions delimited by braces, or another while or for loop.
For example:
var x, y;
for (y=0; y<7; y++)
for (x=0; x<y; x++)
Canvas2D.drawImage(sprites.lives,
new Vector2(x * sprites.lives.width, y * sprites.lives.height));
In this fragment, the variable y counts from 0 to 7. For each of these values of y , the body is
executed, which consists of a for instruction. This second for instruction uses the counter x , which
has as an upper limit the value of y . Therefore, in each progression of the outer for instruction, the
inner for instruction goes on longer. The instruction that is repeated draws a yellow balloon sprite
at the position calculated by using the values of the x and y counters. The result of this loop is a
number of balloons placed in the shape of a triangle (see Figure 10-4 ).
Figure 10-4. Balloons in a triangle shape
 
Search WWH ::




Custom Search