154
Syntax and Language Fundamentals
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.
To repeat an action while a condition exists:
■
Use the
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” on page 160
.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...