One of the most important aspects about programming is consistency, whether it relates to variable naming schemes, coding standards, or where you place your ActionScript code. Code debugging and maintenance is dramatically simplified if the code is organized and adheres to standards. Formatted code that follows an established set of guidelines is easier to maintain, and easier for other developers to understand and modify. For information on ActionScript coding conventions, see ActionScript 2.0 Best Practices.
It is important to understand where to put your ActionScript: Should it be in the FLA file, should it be put on the server in an external AS file that you "include", or should it be a class written using ActionScript 2.0? After you know the scope of your application, you should understand is how to structure your project's code. For information on choosing an ActionScript version, and how to organize and write that code, see ActionScript 2.0 Best Practices.
A general guideline for code placement is to put all your code in as few places as possible, whether you put it inside a FLA document or in external files. Organizing your code in one place helps you edit projects more efficiently, because you can avoid searching in different places when you debug or modify the ActionScript. The following sections provide more information about where to put your ActionScript.
Whenever possible, put your ActionScript in a single location. If you put code in a FLA file, put ActionScript on Frame 1 or Frame 2 in a layer called actions that is on the topmost layer in the Timeline. Alternatively, you might put all of your code in ActionScript (AS) files. 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". For more information on design patterns, see "Using the MVC Design Pattern".
Despite these rare exceptions, you can usually put all your code in the same location (on a frame, or in AS files). The following are the advantages of this process:
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:
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, and requires students to learn two coding styles and forms of syntax, which is why consistency throughout the learning process has advantages.
Attaching ActionScript to a button called myButton_btn looks like the following ActionScript, and should be avoided:
on (release) {
//do something }
However, placing ActionScript with the same purpose on the Timeline looks like the following code, which is encouraged:
myButton_btn.onRelease = function() {
//do something };
For more information on ActionScript syntax, see Syntax and Language Fundamentals in Flash Help or the Flash 8 LiveDocs (Learning ActionScript 2.0 in Flash > Syntax and Language Fundamentals) as well as ActionScript 2.0 Best Practices.
Note: Different practices apply when using behaviors and screens, which sometimes involves attaching code to objects. For more information and guidelines, see "Comparing Timeline Code with Object Code" and "Organizing Code for Screens".