Understanding the Classpath


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 a SWF file from a 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 file. 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.

Global Classpath

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 a FLA file that's saved on your hard drive in a directory called MyTest, the MyTest directory is recognized as a global classpath for that FLA file. 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.

Note

The global class directory can be found in the Flash 8 program folder. On Windows XP, it's in the following location:

Documents and Settings\[user]\Local Settings\Application Data\Macromedia\ Flash 8\en\ Configuration\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 a FLA file, such as the following, 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:

    var addresses:AddressBook = new AddressBook();


If found, that file is loaded and included in the SWF file. If no such file is found, you get a compile error. You can enter multiple directories in your classpath and they will be searched in the order in which you entered them. After a match is found, the search is stopped, and that match is used.

You can edit the global classpath by adding or removing directories with the following process:

1.

Select Edit > Preferences.

2.

Click the ActionScript menu item.

3.

Click the ActionScript 2.0 Settings button.

4.

Use the options in this dialog box to add to or edit the list of directories in the classpath.

Document-Level Classpath

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 a SWF file. 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 a SWF file during compile.

To edit the document-level classpath, follow these steps:

1.

Select File > Publish Settings.

2.

Select the Flash tab.

3.

Click the Settings button next to ActionScript 2.0.

4.

Use the options in this dialog box to add to or edit the list of directories in the classpath.

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 file 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 file that loops while the data from the class files and the rest of the SWF file are loaded.




Macromedia Flash 8 ActionScript Training from the Source
Macromedia Flash 8 ActionScript: Training from the Source
ISBN: 0321336194
EAN: 2147483647
Year: 2007
Pages: 221

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net