Creating and using classes

As discussed in Using classes: a simple example, a class consists of two parts: the declaration and the body. The class declaration consists minimally of the class statement, followed by an identifier for the class name, then left and right curly braces ({}). Everything inside the braces is the class body, as shown in the following example:

class className {
   // class body
}

You can define classes only in AS files. For example, you can't define a class in a frame script in a FLA file.

Class names must be identifiers--that is, the first character must be a letter, underscore (_), or dollar sign ($), and each subsequent character must be a letter, number, underscore, or dollar sign. The class name must exactly match the name of the AS file that contains it, including capitalization. In the following example, if you create a class called Shape, the AS file that contains the class definition must be named Shape.as:

// In file Shape.as
class Shape {
   // Shape class body
}

All AS class files that you create must be saved in one of the designated classpath directories--directories where Flash looks for class definitions when compiling scripts--that is, in the same directory where the FLA file that refers to the class is stored. (See Understanding the classpath.)

If you are creating multiple custom classes, use packages to organize your class files. A package is a directory that contains one or more class files and resides in a designated classpath directory. Class names must be fully qualified within the file in which it is declared--that is, it must reflect the directory (package) in which it is stored.

For example, a class named myClasses.education.curriculum.RequiredClass is stored in the myClasses/education/curriculum package. The class declaration in the RequiredClass.as file looks like this:

class myClasses.education.curriculum.RequiredClass {
}

For this reason, it's good practice to plan your package structure before you begin creating classes. Otherwise, if you decide to move class files after you create them, you will have to modify the class declaration statements to reflect their new location. For more information on organizing classes, see Using packages.

For more information on creating and using classes, see the following topics:


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001059.html