AS2 OOP: Class File Management
         by senocular  

The Classpath
Flash recognizes certain directories on your hard drive to look for class (and interface) files when publishing a movie. These directories are contained in a list called the classpath. If a class needed by a publishing Flash document is within one of the directories specified in its classpath (subdirectories are not included unless specifically added to the classpath separately), then that class will be successfully compiled and added into the swf. If not, the class is not added and your movie will be kaput.

There are actually two classpaths in Flash. One is the global class path and the other is the local or document-level classpath. The global classpath is defined for all Flash movies you make. It's accessible through Flash's preference settings.

Edit > Preferences... > ActionScript (tab) > ActionScript 2.0 Settings...

The local classpath is specific only to the current Flash document and can be accessed through that document's publish settings.

File > Publish Settings... > Flash (tab) > ActionScript version: Settings...

The local classpath is the one you should really pay most attention to. The global classpath will affect all movies, so you may not want to mess with it too much. In adding (or removing) directories from either, you get the following dialog.

[ editing directories in the classpath ]

Simply use the plus icon to add directories to the classpath and the minus to take them away. Directories can either be specified through absolute paths or by relative paths. Relative paths may be preferred in the case where you move your project to another directory somewhere down the line or possibly pass it on to someone else to use on their own computer.

The up and down arrows allow customization in the order in which these directories are accessed by Flash when searching for .as files. The files on top have precedence over the ones below it. So if you happen to have .as files of the same type (possibly different versions of the same file) the .as file within the highest most directory in the classpath will be used. This applies to both the global and local classpaths. Between the two of them, the document-level local classpath has precedence over the global.

[ flash document uses its own and global paths ]

By default, the document classpath is empty. The global classpath, however, has two initial directories (Windows example given, naming may vary slightly for Mac):

$(LocalData)/Classes
.

The first is a Classes directory within your Flash MX 2004 local settings folder which contains copies of all Macromedia Class definitions. Depending on your operating system, this folder will have a different location on your hard drive.

Windows 2000 or Windows XP:
C:\Documents and Settings\[user]\Local Settings\Application Data\Macromedia\Flash MX2004\[language]\Configuration\Classes

Windows 98:
C:\Windows\Application Data\Macromedia\Flash MX 2004\[language]\Configuration\Classes

Macintosh OS X:
Hard Drive/Users/Library/Application Support/Macromedia/Flash MX2004/[language]/Configuration/Classes

This folder includes all core objects as well as classes such as EventDispatcher and others developed by Macromedia. Unlike EventDispatcher, the Flash core definitions, rather declarations, (Array, MovieClip, Math etc.) are all intrinsic classes, so don't expect to see their implementation. They're there just let the compiler know what Flash is capable of and how those classes and methods should be typed.

Below that in the global classpath is the current directory of the document you're working on (.). This assures that .as files within the same directory of your .fla will be recognized by Flash during publish. Neither of these should really be removed, but you're free to add others if you feel the need.

Note that the current directory path lies beneath the LocalData/Classes path. This means that if you try to over-ride any of the core classes within that Classes directory, you won't be able to through the path of your Flash file. You would either need to place the current directory path (.) above the LocalData/Classes path or do so through your local (document-level) classpath which has precedence over the global classpath. This will allow you to do things such as add more methods to the Math object by creating your own intrinsic Math class in one of the directories specified in your local classpath. Copy the current version over and simply change it to be dynamic or write in the new method declarations you wish to include and implement them within your movie. Core Flash classes are now at your disposal for ruining ;).

You may have also noticed another option at the top of the classpath dialog allowing you to choose an export frame for classes (local classpath only, disabled for global). This is the equivalent of the export in first frame option for linked movieclip symbols. It simply allows you to specify where in your movie - what frame on the main timeline - you want you're classes defined.

When writing classes for your Flash projects, you may need to create classes that use or depend on other classes. A prime example of this is when using inheritance. If one class extends another class, that first class needs to know where to find and access its super class. Individual .as files, however, do not have their own classpaths. They have no say over where Flash needs to look to find the classes they depend on. That job is up to you at the Flash document level of the Flash movie they're used in. If a class requires the use of another class, or any other .as file (such as an interface), that file needs to be accessible by the main Flash document. This means, if a Flash movie is using a certain sub class, then the super class that sub class extends has to be within a directory in the movies classpath. If the Flash movie has access to the class, then so do all the other classes used in that movie.

[ classes recognize others through .fla ]

To further help organization yet some more, Flash also now supports packages which can be used in your classpaths.

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.