AS3 Primer : Packages
If you are used to coding in classes, the first notable difference you will find in AS3 is the usage of packages. Every class definition starts with the keyword “package” and then is followed by the actual class definition(s). In AS3 all classes MUST be placed inside a package such as:
package {
public class Example {
}
}
What exactly is a package?
A package is a simple way of organizing classes (of related content) into groups. It’s similar to the com.xxx.yyy notation and is synonymous with directory in the file system. So, say for example I declare a package like this:
package com {
public class Example {
}
}
This ideally means that the class exists in the directory named com which is relative to the place where my FLA resides. The same applies to the example below:
package com.lah {
}
In this case the class file would exist in a subfolder named lah inside the com folder.
What is the purpose of using packages?
The simple purpose would be to better organize your classes and to avoid conflicts of classes. Say for example you have two classes with the same name which are responsible for doing two different tasks. Lets take a simple example of a login system and our application has two types of users - teachers and students. I would want to package all my teacher management related operations in a package named com.lah.teachermgmt and com.lah.studentmgmt. Considering that the login mechanism for the student and teacher is different I would have two LoginManager classes one in the package com.lah.teachermgmt and yet another in com.lah.studentmgmt. In this case, I can have two classes with the same name but under different packages.
By convention, package names are always lowercase characters as against class names which starts with a capital letter.
Usage Example:
Filename : com\lah\Example.as
package com.lah {
public class Example{
public function Example()
{
trace(”Example”);
}
}
}
Filename : Example.fla
import com.lah.*;
// for importing all the classes in the package com.lah or one can use import
// com.lah.Example; to import only a specific class in a package.
var myExample:Example = new Example(); //traces “Example”






Recent Comments