ActionScript can take advantage of three loop types, all of which repeat an action (or set of actions).
The expression
someNumber < 10
is the condition that determines how long the loop will
iterate
(pass through the statement). The actions inside the loop are executed with each iteration. 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._y < 0) {
myClip._y += 3;
}
This script moves the
myClip
MovieClip instance along the
y
axis until its position is greater than or equal to
.
You could also use a
while
loop to loop through an array like this:
var data:Array = ["one", "two", "three"];
var i:Number = 0;
while(i < data.length) {
trace(data[i++]);
}
In the
next
section we will look at a more common way to iterate over predefined number of items, such as an array.
for Loop
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
. 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 10 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:
var data:Array = ["one", "two", "three"];
for(var i:Number = 0; i < data.length ; i++) {
trace(data[i]);
}
for...in Loop
This loop is used to iterate through all of an object's properties. Here's the syntax:
for(var key:String in someObject) {
trace(key);
}
The
key
in the loop is a variable that temporarily stores the
name
of the property referenced by the variable with each loop iteration. The value of
key
can be used in the actions within the loop. For example, let's say you had the following
car
object in your application:
var car:Object = new Object();
car.color = "red";
car.make = "BMW";
car.doors = 2;
But there's a problem: You aren't aware of the properties the car object might have. This could be because your car object is dynamic, meaning the properties can be added at runtime. Therefore, you need a way to inspect the object and retrieve all the properties. The following script will iterate through the object and output the
key
and values for each property on the
car
object:
for(var key:String in car) {
trace(key + ": " + car[key]);
}
On the first iteration of the loop,
key
has a String value of
doors
(because that was the name of the last property defined). During the first loop, the output looks like this:
"doors: 2";
In the expression we trace the output; the variable
key
(without brackets) refers to the property
name
(such as
doors
,
make
, or
color
). Using
car[key]
(that is, placing
key
variable between brackets) is the same as writing
car.doors
and is a reference to that property's
value
.
When the loop is complete, the output panel has the following:
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.
Note
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.