Using packages in flash AS3 (for beginners)
Many people, when starting with flash actionscript 3 developement, first save all the class files to the same folder as the main Flash file (.fla). It works allright when applications are small and you don’t have to share them with anybody.
However, there is a better way to organise your code, that’s called packages ( That’s a fancy object oriented name for subfolders)
Naming the packages
Common method for naming packages ie. folders is to use your companys or your own domain name. It’s convenient method as it provides unique paths for your code. Unique paths prevents your file names clashing with other peoples / companies code when you use their code or they use your classes.
Make folders for your packages
- On the same folder where you have your flash (.fla) file create a subfolder. Name it according to your top-level domain ie. com, net, org etc.
- On that folder make a subfolder named with your domain name. For us the folders would be org\randomaccess\
- Move all your class files to that folder
Declare the new location in your .as class files
On .as files you define the package with dot syntax. For example: ” package org.randomaccess” simply means that “This file is located to a folder org\randomaccess\”
package org.randomaccess{
import flash.display.MovieClip;
public class MyApplication extends MovieClip{
public function MyApplication(){
trace("hello world")
}
}
}
Modify your main .fla to use your class in the new location ie. package
On your flash (.fla) documents properties -> publish -> class write the new location of your main class, with dot syntax.
For example: “org.randomaccess.MyApplication”