So far you have been introduced to the basics of the class syntax and creating a class file. What you haven't yet learned is how class files relate to Flash authoring fileshow they're used in tandem.
If you want to use a custom class in a Flash project to create instances of that class, Flash must know where to find its class file. This section explores how to instruct a Flash file to load and use a custom class. As with many things in Flash, there are several techniques.
When compiling an SWF file from an FLA file, Flash checks to see whether you created an instance of a custom class. If a custom class is being used, Flash attempts to load that custom class to include it inside the SWF file. The directories in which Flash searches for the class are called the classpath. If the class file is found within the classpath, the compiler includes the class in the SWF. If the class file is not found, the compiler reports an error.
Two classpaths are recognized when authoring an FLA file: one classpath is global, and the other is at document level. The global classpath is the same no matter which FLA file you're authoring. The document-level classpath can be changed for a specific FLA file without affecting the classpaths seen by other FLA files.
By default, the global classpath points to two separate directories. The first is the directory in which your current Flash document resides. For example, if you're editing an FLA that's saved on your hard drive in a directory called MyTest, the MyTest directory is recognized as a global classpath for that FLA. You can place class files used by your project in that directory and Flash will find them. The global classpath also points to the directory that contains the class files and interfaces for Flash's built-in classes.
The global class directory can be found in the Flash MX 2004 program folder. On Windows XP, it's in the following location:
Program Files\Macromedia\Flash MX 2004\en\First Run\Classes
Class files placed in this directory are immediately available for use by any FLA you author.
To understand how the classpath works, let's consider an example. If you create an instance of a custom class in an FLA, such as the following:
var addresses:AddressBook = new AddressBook();
during the compiling process (or when you select the Check Syntax option in the Actions panel), the global classpath will be searched for a file called AddressBook.as. If found, that file is loaded and included in the SWF. If no such file is found, you get a compile error.
You can edit the global classpath by adding or removing directories with the following process:
The document-level classpath is empty by default. You can add any number of directories to this classpath to make Flash search for necessary classes while compiling an authoring file to an SWF. This classpath exists only for the current FLA file.
Editing the document-level classpath is a useful option when you want to specify a directory or list of directories that contain class files specific to the current application. If you added these directories to the global classpath, every FLA file or ActionScript file that you edited would include them in the search for the class files to add to an SWF during compile.
To edit the document-level classpath, follow these steps:
Notice that a field called Export Frame for Classes in this dialog box was dimmed in the global classpath version. By default, all class files included in the compiled SWF are initialized on Frame 1 of the movie. Including class files that beef up the SWF file size might cause a lag when loading the SWF file over the Internet, because the data from the class files must be downloaded to the end user's computer before Frame 1 can be rendered.
In many cases, including the class files adds only a tiny amount to the total SWF file size because these files are relatively small. But if you run into a problem when loading this data before Frame 1, you can change the frame number in the Export Frame for Classes field. For example, this would allow you to include a two- or three-frame loading animation in the first few frames of the SWF that loops while the data from the class files and the rest of the SWF are loaded.