Flash AS3 Packages
I’ve been futzing around with Flash lately. This application is the main reason I ditched my abacus and started down the path of web development almost 10 years ago. Alot has changed since Flash 4, though. One of the changes in Flash 9 and AS3 is how you define packages.
A package is nothing more then a namespace which contains a class or multiple classes. In the following example, I have a package of ‘com’ and class inside it called Example. Typically, you name packages with lowercase and classes in CamelCase.
package com {
// import statements go here...
public class Example extends MovieClip {
// declare instance variables here....
// constructor
public function Example() {}
//methods go here...
}
}
OK, great. You’ve created a simple package and class, now what? Well, a package namespace is nothing more then logical representation of your filesystem.
Inside your next flash project, create a folder called ‘com’, and copy and paste the above code into a file called, ‘Example.as’.
/flashproject/com/Example.as
Now, from your actionscript, you can import this class by calling:
import com.Example
Let’s say you create another class called, ‘ExampleToEndAllExamples’ and want to include it an the ‘Example’ class above, just do this:
import com.*
The ‘*’ tells the import statement to include all the classes it finds inside the com namespace.
Now, what to you do if your packages aren’t located inside your flash project folders?
Well, you can always add a classpath to the packages by creating a new entry:
File > Publish Settings > Flash > Settings > Click the Target icon to browse to your packages.
There you go. I hope this helps.