knowledge.lapasa.net

Greater Toronto Area Flash Platform Developer Blog

Notes: Dealing With Packages in AS3.0

TAGS: None

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

ABOUT THE AUTHOR
Mark Lapasa is an experienced (5yrs+) Flex/Flash/ActionScript Developer. He has product experience developing client-side application software for the Financial CRM and Online Gaming Industries.

One Response to “Notes: Dealing With Packages in AS3.0”


  1. Trini
    on Oct 30th, 2009
    @ 11:43 pm

    Thank you for the information. I have created external class files, but cannot load them into my .fla file.

    What is the code for loading external .as class files into my flash file. I would like to create two or three instances of a class at once on stage.

    Thanks, TA

Leave a Reply

© 2009 knowledge.lapasa.net. All Rights Reserved.

This blog is powered by Wordpress and Magatheme by Bryan Helmig.