View comments | RSS feed

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.

Subtopics

Storing code in frames in a Flash timeline
Storing code in ActionScript files

Storing code in frames in a Flash timeline

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).

Storing code in ActionScript files

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.


Flash CS3

Take a survey


Comments


sneakyimp said on Sep 24, 2007 at 10:41 PM :
This explanation says nothing about where flash goes looking for the actionscript files that are imported. For instance, if I import:

com.mydomain.myPackage.*

then where do I have to put the actionscript files so flash can find them?
djtechwriter said on Sep 25, 2007 at 10:04 AM :
In ActionScript 2.0, the package name corresponded to the directory hierarchy relative to the original FLA file. However, with ActionScript 3.0, the package name doesn't necessarily correspond to the directory structure. It may (and usually does) show the directory path, but you can create a package name within your AS file that does not reflect the directory hierarchy. This new flexibility is explained here:

http://livedocs.adobe.com/flash/9.0/main/00000041.html under "Creating packages"
try_test said on Oct 4, 2007 at 9:49 PM :
"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."

Is it means that a class must have uniq name ?
adbe_paul said on Oct 8, 2007 at 1:25 PM :
All the classes within a single package must have unique names.

A class in one package can have the same name as a class in another package. Whichever class is imported with the import statement will determine which class is used. However, if you need to use two different classes that have the same name (and are in different packages) then you'll need to import them and also use the fully-qualified name (package-and-class name) within your code.
lvwarren said on Nov 30, 2007 at 7:27 AM :
that was wonderful, now let's try this:

Options for organizing your code:
1) script include: <mx:Script source="Box.as" />
2) include statement "include Box.as"
3) package reference "import Box"
4) compiled reference .... ?
MotherBrain said on Dec 21, 2007 at 8:00 AM :
If I understand this correctly. When you import a package into your fla file, the package name has no bearing on the import statement whatsoever?

For instance:
Ball.as is saved in a folder called classes.

package toybox
{
public class Ball extends MovieClip
...
}

Inside the fla file, I would do the following correct?

import classes.Ball;

Thus toybox is not utilized? If this is correct, what is reasoning behind using package names?
djtechwriter said on Dec 21, 2007 at 4:51 PM :
MotherBrain,

Actually, that's backwards; you can put your files in any directory hierarchy you want. What matters is how you declare your packages and what classes you declare within each package. So, you'd actually use
import toybox.Ball;

This page should help:
http://livedocs.adobe.com/flash/9.0/main/00000041.html

 

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