ActionScript can take advantage of three loop types, all of which perform an action (or set of actions) while a specific condition is true.
The syntax for creating this common type of loop is as follows:
while (someNumber < 10) { // perform these actions }
The expression someNumber < 10 is the condition that determines the number of iterations (passes through the statement) that the loop will perform. With each iteration, the actions inside the loop are executed. The logic that determines how the condition is evaluated (and how the loop is exited) is discussed shortly, in the section "Writing and Understanding Loop Conditions."
Here's an example of the while loop:
while (myClip_mc._y < 0) { myClip_mc._y += 3; }
This script moves the myClip_mc movie clip instance along the y axis until its position is greater than 0.
The for loop is a compact, all-in-one looping solution for loops that rely on incrementing or decrementing a variable. The for loop lets you initialize a loop variable, set the loop condition, and increment or decrement that variableall in one line of ActionScript. The for loop is typically used to perform an action or set of actions based on the value of an incremental variablewalking an array, for example, or applying actions to a list of movie clips. Here's the syntax of the for loop:
for (var someNumber:Number= 0; someNumber < 10; ++someNumber) { // perform these actions }
The three elements separated by semicolons within the parentheses are used to specify the number of iterations the loop will perform. In this example, the variable someNumber is created and assigned an initial value of 0. The script states next that as long as someNumber is less than 10, the loop executes the actions contained in the loop. The last element in the parentheses specifies that someNumber will be incremented by 1 with each loop iteration, eventually causing someNumber to have a value of 10, which means that the loop will cease after nine iterations.
The for loop is structured to be used primarily to loop through a set of actions a specified number of times. Here's the same example given for the earlier while loop, but now using the for loop syntax:
for (var i:Number=0; i<=10 ; ++i) { myClip_mc.duplicateMovieClip("myClip_mc" + i, i); }
This for loop duplicates myClip_mc 10 times.
This loop is used to loop through all of an object's properties. Here's the syntax:
for (var i:String in someObject) { trace(i); }
The i in the loop is a variable that, with each loop iteration, temporarily stores the name of the property referenced by the variable. The value of i can be used in the actions within the loop. For a practical application, consider the following script:
var car:Object = new Object(); car.color = "red"; car.make = "BMW"; car.doors = 2; var result:String; for (var i:String in car) { result += i + ": " + car[i] + newline; }
First, a generic object is created and named car. The next three lines assign properties (think of them as variables within the car object) and corresponding values to the car object. Next, a for…in loop is set up to loop through all of the car object's properties, using i as the variable that temporarily stores the name of each property. The value of i is used in the action within the loop. When the loop is finished, result holds a string of text that contains the name of each property as well as its value.
On the first iteration of the loop, i has a String value of doors (because that was the name of the last property defined). During the first loop, the expression that sets the value of result looks like this:
result = result + "doors" + ": " + 2 + newline;
After the first loop, result will have this String value:
"doors: 2"
In the expression that sets the value of result, the variable i (without brackets) refers to the property name (such as doors, make, or color). Using car[i] (that is, placing i between brackets) is the same as writing car.doors, and is a reference to that property's value.
When the loop is complete, result will have the following String value:
"doors: 2 make: BMW color: red"
Because the car object has three properties, the for…in loop in this script will perform only three iterations automatically.
NOTE
When you create a property on an object, it's stored in an associative array. In a regular array, elements are referenced by number, starting with 0. In contrast, elements in an associative array are referenced by name. The for…in loop in this section loops through the associative array that contains all of these references in a specific timeline or object.
This type of loop has a variety of uses. You might use a for…in loop to find information, for example, such as the following details:
Name and value of every variable in a timeline or object
Name of every object in a timeline or object
Name and value of every attribute in an XML document