User Guide
ActionScript coding standards 83
Keeping actions together
Whenever possible, put your ActionScript in a single location. If you put code in a FLA file, put
ActionScript on the first or second frame on the Timeline, in a layer called actions that is the first
or second layer on the Timeline. Sometimes you might create two layers for ActionScript to
separate functions, which is an acceptable practice. Some Flash applications do not always put all
your code in a single place (in particular, when you use screens or behaviors). For more
information on organizing ActionScript in an application, see “Organizing files and storing code”
on page 125. For more information on design patterns, see “Using design patterns” on page 104
and “Using the MVC design pattern” on page 126.
Despite these rare exceptions, you can usually put all your code in the same location. The
following are the advantages of this process:
• Code is easy to find in a potentially complex source file.
• Code is easy to debug.
One of the most difficult parts of debugging a FLA file is finding all the code. After you find all
the code, you must figure out how it interacts with other pieces of code as well as the FLA file. If
you put all your code in a single frame, it is much easier to debug because it is centralized, and
these problems reduce in number. For information on attaching code to objects (and
decentralizing your code), see “Attaching code to objects” on page 83. For information on
behaviors and decentralized code, see “Using behaviors” on page 106.
Attaching code to objects
Avoid attaching ActionScript to objects in a FLA file, even in simple SWF files. Attaching code to
an object means that you select a movie clip, component, or button instance; open the Actions
panel; and add ActionScript using the
on() or onClipEvent() handler functions.
This practice is strongly discouraged for the following reasons:
• ActionScript that is attached to objects is difficult to locate, and the FLA files are difficult to
edit.
• ActionScript that is attached to objects is difficult to debug.
• ActionScript that is written on the Timeline or in classes is more elegant and easier to build
upon.
• ActionScript that is attached to objects encourages poor coding style.
• ActionScript that is attached to objects forces students and readers to learn different coding
styles, additional syntax, and a poor and limited coding style. This can be a frustrating
experience.
Some Flash users might say it is easier to learn ActionScript by attaching code to an object because
it might be easier to add simple code, or write about or teach ActionScript this way. A significant
problem exists because most people learning ActionScript need to know how to write the
equivalent code, which is not difficult. The contrast between two styles of coding can also be
confusing to people learning ActionScript, which is why consistency throughout the learning
process has advantages.