This post on classes and instances begins my little expos� on ActionScript 2.0 and Object Oriented Programming. I won't be going into tons of detail (for that you'll have to buy the forthcoming "FlashMX 2004 Demystified" book from Macromedia Press!), but I'll be giving a basic introduction.

I'll also be covering a lot of these materials at FlashParade in October, and more briefly at the next Edmonton Flash User Group meeting on September 15.

Read on for the real content... :)

In ActionScript 2.0, we have finally been blessed with an actual "class" keyword, which thankfully means that you no longer have to define classes as bastardized functions. Defining functions is really easy too, just create a class block, and put the methods and properties you wish to assign to the class inside its brackets:

class GThing {
  // ain't nuthin but a GThing, baby.
   var pureGoodness = true;
   function canIGetSomeLove() {
      return"love";
    }
}

I won't go into details about typing and actually defining properties and methods here - I'll cover it separately in another post. Suffice it to say, classes are nice and easy to define, it's a little trickier knowing where to define them...

In AS2.0, every class must be defined in an external .as file with exactly the same name as the class (ex. "GThing.as"). This class file is then placed inside of a "classPath", the most simple of which is just the same directory as the FLA using the class.

Once you have saved the .as file, you can create instances of it easily from within your FLA the same way you do in AS1:

myGThing = new GThing();
trace("I gotst some "+myGThing.canIGetSomeLove());

FlashMX 2004 sees the reference to the GThing class, and searches it's classPaths for a file called GThing.as. When it finds it, it imports it into the compiled SWF, and makes it available for creating instances.

Next time on AS the OOP turns... defining properties and methods with strong typing. Stay tuned.

Check out some basic OOP concepts in my Flash in the Can 2003 notes.