Controlling when ActionScript runs
When you write a script, you use the Actions panel to attach the script to a frame on a Timeline,
or to a button or movie clip on the Stage. Scripts attached to a frame run, or execute, when the
playhead enters that frame. However, scripts attached to the first frame of a SWF file may behave
differently from those attached to subsequent frames, because the first frame in a SWF file is
rendered incrementally—objects are drawn on the Stage as they download into Flash Player—and
this can affect when actions execute. All frames after the first frame are rendered all at once, when
every object in the frame is available.
Scripts attached to movie clips or buttons execute when an event occurs. An event is an occurrence
in the SWF file such as a mouse movement, a keypress, or a movie clip being loaded. You can use
ActionScript to find out when these events occur and execute specific scripts depending on the
event. For more information, see
To perform an action depending on whether a condition exists, or to repeat an action, you can use
,
,
if
else
else if
described in the rest of this section.
Checking a condition
Statements that check whether a condition is
condition exists, ActionScript executes the statement that follows. 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
perform if previous conditions are false.
if (password == null || email == null) {
gotoAndStop("reject");
} else if (password == userID){
gotoAndPlay("startMovie");
}
If you want to check for one of several conditions, you can use the
using multiple
else if
Repeating an action
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
In the following example, the loop executes four times:
i = 4;
while (var i > 0) {
my_mc.duplicateMovieClip("newMC" + i, i );
i--;
}
56
Chapter 3: Writing and Debugging Scripts
Chapter 4, "Handling Events," on page
,
,
,
for
while
do while
statements.
,
, and
for
for..in
statement.
. After each statement in the body is executed, the expression is evaluated again.
,
, or
for..in
switch
or
begin with the term
true
false
else if
actions to create loops.
83.
statements, which are briefly
. If the
if
specifies alternative tests to
statement instead of
switch
Need help?
Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?