For more information on the
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" on page
ActionScript 2.0 Language Reference.
About creating and ending loops
The following example shows a simple array of month names. A
the number of items in the array and displays each item in the Output panel.
var monthArr:Array = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var i:Number;
for (i = 0; i < monthArr.length; i++) {
trace(monthArr[i]);
}
When you work with arrays, whether they're simple or complex, you need to be aware of a
condition called an infinite loop. An infinite loop, as its name suggests, is a loop with no end
condition. This causes real problems—crashing your Flash application, causing your Flash
document to stop responding in a web browser, or causing very inconsistent behavior of your
Flash document. The following code is an example of an infinite loop:
// BAD CODE- creates an infinite loop
// USE AT OWN RISK!
var i:Number;
for (i = 0; i < 10; i--) {
trace(i);
}
The value of
is initialized to 0 and the end condition is met when
i
to 10 and after each iteration the value of
error immediately: if the value of
never met. The results vary on each computer you run it on, and the speed at which the code
fails depends on the speed of the CPU and other factors. For example, the loop executes about
142,620 times before displaying an error message on a given computer.
The following error message is displayed in a dialog box:
A script in this movie is causing Flash Player to run slowly. If it
continues to run, your computer may become unresponsive. Do you want to
abort the script?
156
Syntax and Language Fundamentals
statement, see
for..in
160, and their respective entries in the
is decremented. You can probably see the obvious
i
decreases after each loop iteration, the end condition is
i
"Using for..in loops" on page
loop iterates from
for
is greater than or equal
i
158.
to
0
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?