Flash CS3 Documentation |
|||
| Programming ActionScript 3.0 > Getting started with ActionScript > Building applications with ActionScript > Options for organizing your code | |||
You can use ActionScript 3.0 code to power everything from simple graphics animations to complex client-server transaction processing systems. Depending on the type of application you're building, you may prefer to use one or more of these different ways of including ActionScript in your project.
In the Flash authoring environment, you can add ActionScript code to any frame in a timeline. This code will be executed while the movie is playing back, when the playhead enters that frame.
Placing ActionScript code in frames provides a simple way to add behaviors to applications built in the Flash authoring tool. You can add code to any frame in the main timeline or to any frame in the timeline of any MovieClip symbol. However, this flexibility comes with a cost. When you build larger applications, it becomes easy to lose track of which frames contain which scripts. This can make the application more difficult to maintain over time.
Many developers simplify the organization of their ActionScript code in the Flash authoring tool by placing code only in the first frame of a timeline, or on a specific layer in the Flash document. This makes it easier to locate and maintain the code in your Flash FLA files. However, in order to use the same code in another Flash project, you must copy and paste the code into the new file.
If you want to be able to use your ActionScript code in other Flash projects in the future, you will want to store your code in external ActionScript files (text files with the .as extension).
If your project involves significant ActionScript code, the best way to organize your code is in separate ActionScript source files (text files with the .as extension). An ActionScript file can be structured in one of two ways, depending on how you intend to use it in your application.
ActionScript written in this way can be accessed using the include statement in ActionScript, or the <mx:Script> tag in Adobe Flex MXML. The ActionScript include statement causes the contents of an external ActionScript file to be inserted at a specific location and within a given scope in a script, as if it were entered there directly. In the Flex MXML language, the <mx:Script> tag lets you specify a source attribute that identifies an external ActionScript file to be loaded at that point in the application. For example, the following tag will load an external ActionScript file named Box.as:
<mx:Script source="Box.as" />
When you define a class, you can access the ActionScript code in the class by creating an instance of the class and using its properties, methods, and events, just as you would with any of the built-in ActionScript classes. This requires two parts:
import statement to specify the full name of the class, so the ActionScript compiler knows where to find it. For example, if you want to use the MovieClip class in ActionScript, you first need to import that class using its full name, including package and class: import flash.display.MovieClip;
Alternatively, you can import the package that contains the MovieClip class, which is equivalent to writing separate import statements for each class in the package:
import flash.display.*;
The only exceptions to the rule that a class must be imported if you refer to that class in your code are the top-level classes, which are not defined in a package.
|
NOTE |
|
In Flash, for scripts attached to frames on the Timeline, the built-in classes (in the flash.* packages) are automatically imported. However, when you write your own classes, or if you're working with Flash authoring components (the fl.* packages) or if you're working in Flex, you will need to explicitly import any class in order to write code that creates instances of that class. |
var smallBox:Box = new Box(10,20);
When the compiler comes across the reference to the Box class for the first time, it searches the loaded source code to locate the Box class definition.
Flash CS3
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000028.html
Comments
sneakyimp said on Sep 24, 2007 at 10:41 PM :