56
Chapter 3: Writing and Debugging Scripts
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
Chapter 4, “Handling Events,” on page 83
.
To perform an action depending on whether a condition exists, or to repeat an action, you can use
if
,
else
,
else if
,
for
,
while
,
do while
,
for..in
, or
switch
statements, which are briefly
described in the rest of this section.
Checking a condition
Statements that check whether a condition is
true
or
false
begin with the term
if
. If the
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
else if
specifies alternative tests to
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
switch
statement instead of
using multiple
else if
statements.
Repeating an action
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.
In the following example, the loop executes four times:
i = 4;
while (var i > 0) {
my_mc.duplicateMovieClip("newMC" + i, i );
i--;
}
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...