9.3 Package Access and the Classpath

 <  Day Day Up  >  

In order for a .fla file to access a class in a package, the root directory of the package must reside within the classpath . The classpath is simply the list of directories (paths) on your hard drive in which the compiler looks for classes. Entries in the classpath can be added globally for all documents (the global classpath ) or on a per- .fla file basis (a document classpath ).

By default, the global classpath includes the following directories:

  • The current directory, usually represented by a period (.) ”i.e., the directory in which the .fla resides

  • The Classes directory, $(LocalData)/Classes ” that is, the Classes directory in the user configuration directory for the Flash MX 2004 or Flash MX Professional 2004 application

The Classes directory is found in one of the following locations, depending on your operating system. In each of the following paths, substitute your operating system user account name for USER and your Flash language code for LANGUAGE_CODE , which is "en" for English:


Windows 2000, Windows XP

C:\Documents and Settings \ USER \Local Settings\Application Data\Macromedia\Flash MX 2004 \ LANGUAGE_CODE \ Configuration\Classes

Note that the Local Settings folder is hidden by default but can be revealed in Windows Explorer using Tools Folder Options View Advanced Settings Files and Folders Hidden Files and Folders Show Hidden Files and Folders.


Windows 98

C:\Windows\Application Data\Macromedia\Flash MX 2004\ LANGUAGE_CODE \Configuration\Classes


Macintosh OS X

Hard Drive/Users/USER/Library/Application Support/Macromedia/Flash MX 2004/ LANGUAGE_CODE /Configuration/Classes

Because the default global classpath includes the current directory (.), every .fla file can access classes in the same directory as the .fla itself. Similarly, every .fla file can access classes in the /Configuration/Classes directory. You should not remove either of those directories from the global classpath.

Furthermore, if the root directory of a package resides in either the same directory as the .fla file or in the /Configuration/Classes directory, then the classes in that package are accessible to the .fla file.

For example, suppose that, on Windows XP, we create a document, game.fla and place it in the directory c:\data\projects\fungame . Then we create a class, com.yourdomain.game.Player and place its class file in: c:\data\projects\fungame\com\yourdomain\game\Player.as . Because game.fla and the root of the Player class's package reside in the same directory, game.fla finds the Player class successfully.

 // Given this directory setup,   game.fla   can access the   Player   class C:\data\projects\fungame\game.fla C:\data\projects\fungame\com\yourdomain\game\Player.as 

Likewise, if the class file for com.yourdomain.game.Player resides in the /Configuration/Classes directory, then game.fla (and, indeed, any .fla file) will find the Player class successfully.

 // Given this directory setup,   game.fla   can access the   Player   class C:\data\projects\fungame\game.fla C:\Documents and Settings\   USER   \Local Settings\Application Data\ Macromedia\Flash MX 2004\   LANGUAGE_CODE   \Configuration\Classes\com\yourdomain \game\Player.as 

If Player.as is not found in either of those locations, the compiler generates the following error:

 The class 'com.yourdomain.game.Player' could not be loaded. 

It's not a good idea to store your class files in the /Configuration/Classes directory. That directory can easily be confused with a similarly named directory in Flash's application folder, and it is awkward to include in routine data backups . You should store your class files with the rest of your project data (images, videos , sounds, .fla files, etc.). Classes used across multiple projects should be stored in a central location, such as c:/data/actionscript .


9.3.1 Adding a New Directory to the Classpath

If we want to store our Player.as class file in a directory other than the two included by default in the global classpath (and /Configuration/Classes ), we have to add the package root directory to either the global classpath or the document classpath (i.e., the classpath specified by game.fla itself). For example, suppose we were to place Player.as here:

 C:\data\actionscript\com\yourdomain\game\Player.as 

To give all .fla files access to the Player class, we'd add the directory C:\data\actionscript to the global classpath, as follows :

  1. Select Edit Preferences ActionScript Language ActionScript 2.0 Settings.

  2. Under Classpath, click the plus sign (+) to add a new classpath entry. A blank entry appears.

  3. In the classpath entry, type C:\data\actionscript\ . (Or use the crosshair button to browse to that directory.)

  4. Click OK to accept the ActionScript 2.0 settings.

  5. Click OK to accept the new preferences.

Alternatively, if we were using the Player class only in game.fla , we could give game.fla individual access to the Player class by adding the directory C:\data\actionscript to game.fla 's own classpath, like this:

  1. Open game.fla in Flash MX 2004.

  2. Select File Publish Settings Flash ActionScript 2.0 Settings.

  3. Follow Steps 2-5 in the preceding procedure.

When more than one project uses the same classes, it's sensible to store them in a central location. For information on storing classes centrally , see Chapter 14.


Classpath entries can also be specified using relative paths, such as ./ for the directory that contains the .fla file, ../ for one directory up from the .fla file, ../../ for two directories up, and so on. For example, if your .fla file is stored in this location:

 C:\data\projects\fungame\movie\game.fla 

and the classes used by game.fla are stored in this location:

 C:\data\projects\fungame\classes 

you can grant game.fla access to the classes in the classes directory by specifying the following relative classpath:

 ../classes 

Relative classpaths are useful when sharing files across disparate filesystems or operating systems, as is common when working with a team or using version control software.

 <  Day Day Up  >  


Essential ActionScript 2.0
Essential ActionScript 2.0
ISBN: 0596006527
EAN: 2147483647
Year: 2004
Pages: 177
Authors: Colin Moock

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