User Guide
140 Chapter 4: 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 can 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 scripts execute. All frames after the first frame are rendered at the same time,
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 5, “Handling Events,” on page 167.
Using the Actions panel and Script window
You can embed Flash scripts in your FLA file or store them as external files. It’s a good idea to
store as much of your ActionScript code in external files as possible, which makes it easier to reuse
code in multiple FLA files. In your FLA file, you can create a script that uses
#include statements
to access the code you’ve stored externally. Use the .as suffix to identify your scripts as
ActionScript (AS) files.
You can use ActionScript 2.0 to create custom classes. You must store custom classes in external
AS files and use
import statements in a script to get the classes exported into the SWF file,
instead of using
#include statements. For more information on ActionScript 2.0 and importing
classes, see “New object-oriented programming model” on page 21 and “Importing classes”
on page 271.
Note: ActionScript code in external files is compiled into a SWF file when you publish, export, test, or
debug a FLA file. Therefore, if you make any changes to an external file, you must save the file and
recompile any FLA files that use it.
When you embed ActionScript code in your FLA file, you can attach code to frames and to object
instances (such as movie clips). One method is to attach embedded ActionScript to the first frame
of the Timeline whenever possible so you don’t have to search through a FLA file to find all your
code; it is centralized in one location. Another method is to create a layer called Actions and place
your code there. If you place code on other frames or attach it to objects, you can find your code
on that layer.
To create scripts that are part of your document, you enter ActionScript directly into the Actions
panel. To create external scripts, use your preferred text editor or, in Flash Professional, you can
use the Script window (File > New > ActionScript File). When you use the Actions panel or Script
window, you are using the same ActionScript editor and are typing your code in the Script pane at
the right side of the panel or window. Instead of typing code into the Actions panel, you can also
select or drag actions from the Actions toolbox to the Script pane.