About statements
157
When you work with loops (and especially
while
and
do..while
loops), always make sure
that the loop can exit properly and does not end up in an infinite loop.
For more information on controlling loops, see
“Using a switch statement” on page 147
.
Using for loops
The
for
loop lets you iterate over a variable for a specific range of values. A
for
loop is useful
when you know exactly how many times you need to repeat a series of ActionScript
statements. This can be useful if you want to duplicate a movie clip on the Stage a certain
number of times or to loop over an array and perform a task on each item in that array. A
for
loop repeats an action using a built-in counter. In a
for
statement, the counter and the
statement that increments the counter are all part of the
for
statement. You write the
for
statement using the following basic format:
for (init; condition; update) {
// statements;
}
You must supply three expressions to a
for
statement: a variable that is set to an initial value,
a conditional statement that determines when the looping ends, and an expression that
changes the value of the variable with each loop. For example, the following code loops five
times. The value of the variable
i
starts at
0
and ends at
4
, and the output are the numbers
0
through
4
, each on its own line.
var i:Number;
for (i = 0; i < 5; i++) {
trace(i);
}
In the next example, the first expression (
i = 0
) is the initial expression that evaluates before
the first iteration. The second expression (
i < 5
) is the condition that you check each time
before the loop runs. The third expression (
i++
) is called the
post expression
and is evaluated
each time after the loop runs.
To create a for loop:
1.
Select File > New and then select Flash Document.
2.
Create a movie clip on the Stage.
3.
Right-click the movie clip symbol in the Library panel and select Linkage from the
context menu.
4.
Select the Export for ActionScript check box, and type
libraryLinkageClassName
in the
Class text input field. Click OK.
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...