Notes: Dealing With Packages in AS3.0

In AS2.0, you would structure the class files of a project as one .as file for each class. Each .as file would declare a single class.

// Circle.as
class Circle
{
public function Circle(){}; // constructor
}

Recall, packages in AS2.0 represented the directories where the source files resided. In AS3.0, this hasn’t changed either. If Circle.as was an ActionScript class file that was under the Shapes package which is under the Graphics package, you would need to prefix the class declaration with the fully qualified name of the package to satify the compiler:

// Graphics/Shapes/Circle.as
class Graphics.Shapes.Circle
{
public function Circle(){}; // constructor
}

What is new in AS3.0 is that each .as file now must have a package block. Adobe has made it clear that this is not just for the sake of the traditional establishment of namespace for a class’s elements so that it doesn’t collide with other objects that have identical element names. It is for added flexibility to the responsibilities a class will exercise. The new .as file format allows you to have one public class definition per source file but that source file can have many private support classes. The existence of these other classes are to support the one class that is declared public. Helper classes are declared outside of the package block. Furthermore, they cannot be acccessed publically.

// Circle.as
package Graphics.Shapes
{
public class Circle
{
public function Circle(){}; // constructor
}
}
class ColorCircle
{
// If ColorCircle class was declared inside the package block, the compiler would throw an error
}

Defining classes outside of a package will make that access specifier of type ‘internal’. This means it is only available to classes of the same package. The code inside ColorCircle would not be available to code outside of the Graphics.Shapes package.

Source
pg 42 - “Creating Packages” from Programming ActionScript 3.0

Leave a Reply