28th Apr 2008

Setting Up A Global Class Path In Flash

If you are just getting into using classes in Flash, chances are you are probably just creating your class (.as files) in the same directory as your FLA. This is simple, fast and works, but if I want to reuse a class from one Flash piece in another, I now have to copy over all of those class files into my new project directory. If I use that methodology, if I have to update something in one of my classes, I now have to update them in more than one location. This quickly turns into a maintenance nightmare and it turns out there is a simple solution.

Flash (as far back as mx 2007, not sure about older versions) will allow you to select a folder(s) where you can store classes that are used across multiple projects. This is commonly referred to as a class path. When you set up a class path and then do something like:


import com.mydomain.MyClass;
var myInstance:MyClass = new MyClass();

where MyClass is some custom class that you wrote, Flash will now look in your specified class path to see if it can find the corresponding class file (in this case MyClass.as). This means I am now free to use this class in any of my projects without having copy my class file into each project directory.

Please note that I am showing you how to set this for the program as a whole. You can also set this on a per document basis using the publishing settings

So how do we set this little piece of magic up?

  1. Got to the Edit menu and select Preferences.
  2. The following dialog box should appear:
    Flash Preferences
  3. Select ActionScript from the list on the left.
  4. Click the button entitled “ActionScript [version number] Settings” next to the Language label and the following dialog box should appear.
  5. Make sure to hit the + button resetting the entry that reads $(LocalData)/Classes will reset the default class path for Flash and Flash will no longer be able to find its built in classes. Clicking the + sign should make a blank line appear in the list.
  6. Click on the icon that looks like cross hair. This should bring up a file selection dialog box.
  7. Select the folder where you would like to store your classes.
  8. Keep clicking OK until all of the dialog boxes have closed.

Now you have your class path set up. This means that you can start storing you class files in this location. Please take note of the following:

  • Now that you have the class path set up, you can use import statements to bring in your classes just like you do with the standard Flash classes. For example import com.mydomain.MyClass; . Note a few things about the import statement.
    • The actual folder you selected is NOT included in the import statement (in my example C:\asclasses)
    • In import statements, dots = slashes. What I mean by this is that in our example, com.mydomain.MyClass is looking a file named MyClass.as in a folder mydomain that is a subfolder of com (i.e. /com/mydomain/MyClass.as). The beginning folder com in turn is actually a sub folder of the class path (i.e. C:\asclasses\com\mydomain\MyClass.as). Since we have setup C:\asclasses as a class path, Flash automatically looks in that location and we only have to specify the path using the dot syntax from that folder down. If you want to learn more about this dot syntax, do some research on the topic of packages in Flash.
    • As you may have noticed, you leave off the .as file extension.

Now that you have you class path set up, you can truly begin to start creating classes that are reusable across projects and only have to be maintained in one location.

2 Responses to “Setting Up A Global Class Path In Flash”

  1. loucostello Says:

    Thanks. Odd how hard it is to find the real “basics” explained clearly rather than just shrugged off. Much appreciated.

    FYI – Redirected here from http://board.flashkit.com/board/showthread.php?t=774106, comment by you.

  2. Jeremy Wischusen Says:

    Not a problem. Glad someone is finding it useful. If there are any other subjects you think people could benefit from, please let me know.