AS2 OOP: Class File Management
         by senocular  

Import
In using packages, especially the more complex ones, class referencing can get a little out of hand. You may have 4 or 5 or even more object scopes to go through before you get to the class you need to use. The new ActionScript 2.0 import command helps ease this burden.

What import does is allows for a temporary escape of a class from its packaged namespace. This means taking a long class reference which is buried deep within a package and temporarily, within the duration of the current script, reducing its lengthy reference to just being the classname itself. Just use import with the path of the package you want to ... import.

import path;

We can use the Bombatoss class as an example.

import Enemies.Bosses.Bombatoss;
var badGuy:Bombatoss = new Bombatoss(100);

Because its temporary, you can only use Bombatoss in this manner for the duration of that script and that script alone. If this was used in frame 1 of the main timeline, Bombatoss would not be directly accessible on frame 2 - not without using import again.

If you're dealing with a lot of classes in a certain package scope, you can import them all at once using a wildcard (*) to indicate importing all files within a directory.

import Enemies.Bosses.*;
var badGuy1:Bombatoss = new Bombatoss(100);
var badGuy2:Grindinator = new Grindinator(100);
// ... same for all classes in Bosses

Try it yourself! (zipped source)

Import also works within other class files.

import Enemies.Bosses.*;
class Person {
var nemesis:Grindinator;
function Person (enemy:Grindinator) {
nemesis = enemy;
}
}

Remember, the classes used by the Person example above have to be accessible to the .fla before the class itself can access it.

The new EventDispatcher is in a package. Its contained within directory

Classes\mx\events

Where Classes is the path specified in Flash's global classpath. This means that mx is the package in which EventDispatcher exists. Sure enough, if you look at the class file for EventDispatcher you'll see it defined as

class mx.events.EventDispatcher { ...

If you ever need to use the EventDispatcher class, you would need to use its full mx.events... name. That is of course unless you use import...

import mx.events.EventDispatcher;
// or
import mx.events.*;

Be careful not to confuse import with #include. They are not the same or even all that similar.

 




SUPPORTERS:

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