User Guide

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 doesnt
exist, ActionScript skips to the next statement outside the block of code.
To optimize your codes 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.