Flash MX 2004: AS2 Classes Require Global Classpath

This one was hard to find, and painful to find out. If you want your AS2 classes to contain references to other AS2 classes that are in a package namespace, then you must add the root of the package namespace to your global classpath in Flash or you will get compile-time errors that cryptically indicate something’s wrong. This is bad from a software engineering perspective.

Java != ActionScript 2

The reason this sucks is that there’s no concept of a “Project” classpath so all your classes have to be in the same global location across projects or you have to keep appending project-specific directories to your global classpath. Either way, it’s an incomplete solution to the need for package isolation within a particular build. If you want to have multiple versions of a project around…you’ve got to change the global classpath?

Here’s an example of what doesn’t work without a global classpath addition:


import com.h2co3.project.*;

class com.h2co3.project.Survey {
    
    /* Items in this survey */
    var items:Array;
    
    function Survey() {
        items = new Array();
    }
    
    function addSurveyItem(item:SurveyItem) {
        items.push(item);
    }
}

Notice that the second class (com.h2co3.project.SurveyItem) is used as a parameter in a method (err….function) declaration. In order for the compiler to resolve this properly, it has to know where to find the dang thing. It turns out that Flash documents, as in *.fla, have a default classpath that will look for AS2 classes under the working directory — but not the compiler for other classes defined externally.

Coming from Java, this is a big surprise and a big drawback. But…it’s a work in progress and it’s progressing in the right direction. I just wish the error messages were more informative and it was easier to find this out.