Creating and packaging your class files

In this section, you create, name, and package your class files for this example (Example: Writing custom classes). The following sections show you how to write complete (yet simple) class files. For detailed information on packages, see About packages, A comparison of classes and packages, and Working with packages.

When you create a class file, decide where you want to store the file. In the following steps, you'll save the class file and the application FLA file that uses the class file in the same directory for simplicity. However, if you want to check syntax, you also need to tell Flash how it can find the file. Typically, when you create an application, you add the directory in which you store your application and class files to the Flash classpath. For information about classpaths, see About setting and modifying the classpath.

Class files are also called ActionScript (AS) files. You create AS files in the Flash authoring tool or by using an external editor. For example, Macromedia Dreamweaver can create AS files.

NOTE

 

The name of a class (ClassA) must exactly match the name of the AS file that contains it (ClassA.as). This is very important; if these two names don't match exactly, including capitalization, the class won't compile.

To create a class file and class declaration:

  1. Select File > New and then select Flash Document to create a new FLA document, and then click OK.
  2. Select File > Save As, name the new file package_test.fla, and save the Flash document to the current directory.

    You'll add content to this Flash document in a future step.

  3. Select File > New and then select ActionScript File, and then click OK.
  4. Select File > Save As and create a new subdirectory named com, and then do the following:
    1. In the com subdirectory, create a new subdirectory named adobe.
    2. in the adobe subdirectory, create an new subdirectory named utils.
    3. Save the current ActionScript document in the utils directory and name the file ClassA.as.
  5. Type the following code into the Script window:
    class com.adobe.utils.ClassA {
    }
    

    The preceding code creates a new class named ClassA in the com.adobe.utils package.

  6. Save the ClassA.as ActionScript document.
  7. Select File > New and then select ActionScript File, and then click OK.
  8. Select File > Save As, name the new file ClassB.as, and save it in the same directory as ClassA.as created in an earlier step.
  9. Type the following code into the Script window:
    class com.adobe.utils.ClassB {
    }
    

    The previous code creates a new class named ClassB in the com.adobe.utils package.

  10. Save your changes to both the ClassA.as and ClassB.as class files.

The class files you use in a FLA file import into a SWF file when you compile it. The code you write in a class file should have a certain methodology and ordering, which are discussed in the following sections.

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. A class name 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 more information on classpaths, see About setting and modifying the classpath.

For example, a class named com.adobe.docs.YourClass is stored in the com/adobe/docs directory. The class declaration in the YourClass.as file looks like this:

class com.adobe.docs.YourClass {
    // your class
}

NOTE

 

You write the class declaration that reflects the package directory in the following section, Example: Writing custom classes.

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.

To package your class files:

  1. Decide on the package name you'd like to use.

    Package names should be intuitive and easily identifiable by fellow developers. Remember that the package name also matches a specific directory structure. For example, any classes in the com.adobe.utils package needs to be placed in a com/adobe/utils folder on your hard drive.

  2. Create the required directory structure after you've chosen a package name.

    For example, if your package was named com.adobe.utils, you would need to create a directory structure of com/adobe/utils and place your classes in the utils folder.

  3. Use the com.adobe.utils prefix for any class you create in this package.

    For example, if your class name was ClassA, the full class name would need to be com.adobe.utils.ClassA within the com/adobe/utils/ClassA.as class file.

  4. If you change your package structure at a future point, remember to modify not only the directory structure, but the package name within each class file, as well as every import statement or reference to a class within that package.

To continue writing the class files, see Writing the constructor function.


Flash CS3