40
Chapter 1: ActionScript Basics
Checking conditions
Statements that check whether a condition is
true
or
false
begin with the term
if
. If the
condition evaluates to
true
, ActionScript executes the next statement. If the condition doesn’t
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
else if
specifies alternative tests to
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
switch
statement rather than
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
,
for
, and
for..in
actions to create 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.
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 the loop always
runs at least once.
To repeat an action using a built-in counter:
•
Use the
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 = 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 = 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
statement.
Summary of Contents for FLEX-FLEX ACTIONSCRIPT LANGUAGE
Page 1: ...Flex ActionScript Language Reference...
Page 8: ......
Page 66: ...66 Chapter 2 Creating Custom Classes with ActionScript 2 0...
Page 76: ......
Page 133: ...break 133 See also for for in do while while switch case continue throw try catch finally...
Page 135: ...case 135 See also break default strict equality switch...
Page 146: ...146 Chapter 5 ActionScript Core Language Elements See also break continue while...
Page 808: ...808 Chapter 7 ActionScript for Flash...
Page 810: ...810 Appendix A Deprecated Flash 4 operators...
Page 815: ...Other keys 815 Num Lock 144 186 187 _ 189 191 192 219 220 221 222 Key Key code...
Page 816: ...816 Appendix B Keyboard Keys and Key Code Values...
Page 822: ...822 Index...