Actionscript Package

ActionScript packages are just directories to store your classes in to keep you code organized instead of have a lot of classes in the same folder. Packages in ActionScript 3.0 are implemented with namespaces, when you declare a package, you are implicitly creating a special type of namespace.

package packageName{
   public class MyClass{
       
      public var someVar:String;
               
      public function MyClass() {
         //Code goes here
      }
   }
}

The name of the class in this example is MyClass. Because the class is inside the packageName package. Class named MyClass in a package named packageName becomes known as packageName.MyClass. Notice that the package name comes first and is separated from the class name using a period (.) character.

How to create a package.

Packages defines similar classes, for example: if MyClass and MyOtherClass are similar will be in the same package myPackage and that code will be organized in the same folder... MyClass.as and MyOtherClass.as will be in the folder myPackage folder. Also we can nest several packages to organize the code. For example:

/*
In folders:
mainPackage/
   - package1/
      - Class1.as
      - Class2.as
   - package2/
      - Class3.as
*/
       

package mainPackage.package1{
   public class Class1{
      //Code goes here
   }
}

package mainPackage.package1{
   public class Class2{
      //Code goes here
   }
}

package mainPackage.package2{
   public class Class3{
      //Code goes here
   }
}

How to import a package.

If you want to use a class that is inside a package, you must import either the package or the specific class. This differs from ActionScript 2.0, where importing classes was optional.

//To import all classes in "package1"
import package1.*;

//or

//Import only "Class1"
import package1.Class1;

One important issue is if class is public is visible outside the own package, but if class is not public, that class will be visible only within the own ActionScript package.

0
Your rating: None

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd><p>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: [code], [blockcode], [as], [as3], [css], [drupal5], [drupal6], [html], [java], [javascript], [mysql], [php], [xml].
  • You may quote other posts using [quote] tags.
  • Textual smileys will be replaced with graphical ones.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Copy the characters (respecting upper/lower case) from the image.