Chapter 12. Looping over Code

Write a For Loop

Frequently, you will need to perform repetitive tasks in your scripts. Like other programming languages, ActionScript provides for several different methods of looping over a block of code.

One of the most common types of loops is a for loop. To initialize the loop, you need to provide three key elements: the counter variable for the loop, the condition on which the loop will be tested, and a statement to change the value of the counter with each iteration of the loop. Following the initialization line, you will place the code to be executed with each iteration within a set of curly braces. For example, your loop might look like this:

for(var i:Number=0; i<10; i++)
{
     trace(i);
}

In this example, a variable, ...

Get ActionScript®: Your visual blueprint™ for creating interactive projects in Flash® CS4 Professional now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.