
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--;
}
Содержание FLASH MX 2004 - ACTIONSCRIPT
Страница 1: ...ActionScript Reference Guide...
Страница 8: ...8 Contents...
Страница 12: ......
Страница 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Страница 54: ...54 Chapter 2 ActionScript Basics...
Страница 80: ...80 Chapter 3 Writing and Debugging Scripts...
Страница 82: ......
Страница 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Страница 112: ......
Страница 120: ...120 Chapter 6 Using the Built In Classes...
Страница 176: ......
Страница 192: ...192 Chapter 10 Working with External Data...
Страница 202: ...202 Chapter 11 Working with External Media...
Страница 204: ......
Страница 782: ...782 Chapter 12 ActionScript Dictionary...
Страница 793: ...Other keys 793 221 222 Key Key code...
Страница 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Страница 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Страница 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Страница 816: ...816 Index...