Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Syntax and Language Fundamentals > About statements > Repeating actions using loops | |||
ActionScript can repeat an action a specified number of times or while a specific condition exists. Loops let you repeat a series of statements when a particular condition is true. There are four types of loops in ActionScript: for loops, for..in loops, while loops, and do..while loops. Each type of loop behaves somewhat differently, and each one is useful for different purposes.
Most loops use some kind of counter to control how many times the loop executes. Each execution of a loop is called an iteration. You can declare a variable and write a statement that increases or decreases the variable each time the loop executes. In the for action, the counter and the statement that increments the counter are part of the action.
|
Loop |
Description |
|---|---|
|
|
Repeat an action using a built-in counter. |
|
|
Iterate over the children of a movie clip or object. |
|
|
Repeat an action while a condition exists. |
|
|
Similar to while loops, except the expression evaluates at the bottom of the code block, so the loop always runs at least once. |
The most common type of loop is the for loop, which loops over a block of code a predefined number of times. For example, if you have an array of items, and you want to perform a series of statements on each item in the array, you would use a for loop and loop from 0 to the number of items in the array. Another type of loop is the for..in loop, which can be very useful when you want to loop over each name/value pair within an object and then perform some type of action. This can be very useful when you are debugging your Flash projects and want to display the values that load from external sources, such as web services or external text/XML files. The final two types of loops (while and do..while) are useful when you want to loop over a series of statements but you don't necessarily know how many times you need to loop. In this case you can use a while loop that loops as long as a certain condition is true.
ActionScript can repeat an action a specified number of times or while a specific condition exists. Use the while, do..while, for, and for..in actions to create loops. This section contains general information on these loops. See the following procedures for more information on each of these loops.
while statement. A while loop evaluates an expression and executes the code in the body of the loop if the expression is true. After each statement in the body is executed, the expression is evaluated again. In the following example, the loop executes four times:
var i:Number = 4;
while (i > 0) {
myClip.duplicateMovieClip("newMC" + i, i, {_x:i*20, _y:i*20});
i--;
}
You can use the do..while statement to create the same kind of loop as a while loop. In a do..while loop, the expression is evaluated at the bottom of the code block so that the loop always runs at least once.
This is shown in the following example:
var i:Number = 4;
do {
myClip.duplicateMovieClip("newMC" + i, i, {_x:i*20, _y:i*20});
i--;
} while (i > 0);
For more information on the while statement, see Using while loops.
for statement. Most loops use some kind of counter to control how many times the loop executes. Each execution of a loop is called an iteration. You can declare a variable and write a statement that increases or decreases the variable each time the loop executes. In the for action, the counter and the statement that increments the counter are part of the action.
In the following example, the first expression (var i:Number = 4) is the initial expression that is evaluated before the first iteration. The second expression (i > 0) is the condition that is checked each time before the loop runs. The third expression (i--) is called the post expression and is evaluated each time after the loop runs.
for (var i:Number = 4; i > 0; i--) {
myClip.duplicateMovieClip("newMC" + i, i, {_x:i*20, _y:i*20});
}
For more information on the for statement, see Using for loops.
for..in statement. Children include other movie clips, functions, objects, and variables. The following example uses the trace statement to print its results in the Output panel:
var myObject:Object = {name:'Joe', age:25, city:'San Francisco'};
var propertyName:String;
for (propertyName in myObject) {
trace("myObject has the property: " + propertyName + ", with the value: " + myObject[propertyName]);
}
This example produces the following results in the Output panel:
myObject has the property: name, with the value: Joe myObject has the property: age, with the value: 25 myObject has the property: city, with the value: San Francisco
You might want your script to iterate over a particular type of child--for example, over only movie clip children. You can do this using for..in with the typeof operator. In the following example, a child movie clip instance (called instance2) is inside a movie clip instance on the Stage. Add the following ActionScript to Frame 1 of the Timeline:
for (var myName in this) {
if (typeof (this[myName]) == "movieclip") {
trace("I have a movie clip child named " + myName);
}
}
For more information on the for..in statement, see Using for..in loops.
|
WARNING |
|
Iterations in Flash execute very quickly in the Flash Player, but loops depend heavily on the processor. The more iterations a loop has and the more statements executed within each block, the more processor resources will be consumed. Poorly written loops can cause performance problems and stability issues. |
For more information on each statement, see the individual sections that follow in this chapter, such as Using while loops, and their respective entries in the ActionScript 2.0 Language Reference.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000709.html