Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.


When import Does More Than Just Shorten Class References in AS2

Posted October 21, 2006 by senocular

The import statement in ActionScript 2 typically serves only as a name shortener for fully qualified class names. In other words, it allows you to reference a class using just the name of the class rather than the class name along with the package path (assuming the class belongs in a package).

Little do many people know, import also has another handy use. Using import, you are able to override classes contained within pre-compiled components with your own.
Take the V2 ScrollPane component that comes with Flash, for example. In its current state, it has a nice little "feature" which essentially breaks the first click of the arrows on either of the scrollbars. Thankfully, the classes the ScrollPane component use are available to us within the Flash installation directory so we can actually dig in and see where the problem lies. This particular issue can actually be resolved in the ScrollPane's superclass, ScrollView. At around lines 530-540 in mx.core.ScrollView, changing the references "__vPosition" and "__hPosition" to "vPosition" and "hPosition" will provide a fix for this issue.

Of course you can't just change these classes and be done with it. For one, it's never a good idea to edit the classes built in to Flash. If they are ever updated in the future with an upgrade or updater, those edits would be lost. Also, if you are sharing with other people or someone using another computer using a different Flash installation, they would be using the default classes and not the ones you've edited

Additionally, the ScrollPane component exists as a pre-compiled component where the ActionScript it uses is already built in to component itself (SWC). Editing any of the ActionScript files on your hard drive would not affect the component when it's compiled into your resulting SWF.

The solution is two-fold. First, instead of editing the classes installed with Flash, copy them to another folder in your class path, ideally one specific to the project so that it can easily shared with others if needed (such as in the same location as the FLA itself). Secondly, use the import statement to import the path of the components class not necessarily the class you might be editing, but the actual component itself. In fixing the ScrollPane, for example, even though ScrollView is being edited, you would need to import mx.containers.ScrollPane. This is what lets Flash go through and identify your own copies of the classes related to this component and give them precedence over those contained within the compiled component itself.

Here are two SWFs with ScrollPane components. The only difference between them is the second one includes the following ActionScript:
import mx.containers.ScrollPane;

(Note: both are saved in the same directory which also has a fixed mx/core/ScrollView.as)