Checking conditions
Statements that check whether a condition is
condition evaluates to
exist, ActionScript skips to the next statement outside the block of code.
To optimize your code's performance, check for the most likely conditions first.
The following statements test three conditions. The term
perform if previous conditions are false.
if (password == null || email == null) {
gotoAndStop("reject");
} else if (password == userID){
gotoAndPlay("startProgram");
}
If you want to check for one of several conditions, you can use the
multiple
else if
statements.
Repeating actions
ActionScript can repeat an action a specified number of times or while a specific condition exists.
Use the
,
while
do..while
To repeat an action while a condition exists:
•
Use the
while
A
loop evaluates an expression and executes the code in the body of the loop if the
while
expression is
true
You can use the
loop, the expression is evaluated at the bottom of the code block so the loop always
do..while
runs at least once.
To repeat an action using a built-in counter:
•
Use the
statement.
for
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
the statement that increments the counter are part of the action. In the following example, the
first expression (
second expression (
third expression (
for (var i = 4; i > 0; i--){
my_mc.duplicateMovieClip("newMC"+ i, i, {_x:i*20, _y:i*20});
}
To loop through the children of an object:
•
Use the
for..in
40
Chapter 1: ActionScript Basics
, ActionScript executes the next statement. If the condition doesn't
true
,
, and
for
for..in
statement.
. After each statement in the body is executed, the expression is evaluated again.
statement to create the same kind of loop as a
do..while
) is the initial expression that is evaluated before the first iteration. The
var i = 4
) is the condition that is checked each time before the loop runs. The
i > 0
) is called the post expression and is evaluated each time after the loop runs.
i--
statement.
or
begin with the term
true
false
specifies alternative tests to
else if
switch
actions to create loops.
. If the
if
statement rather than
loop. In a
while
action, the counter and
for
Need help?
Do you have a question about the FLEX-FLEX ACTIONSCRIPT LANGUAGE and is the answer not in the manual?