AS2 OOP: Class Structure
         by senocular  

Intrinsic Classes
There is yet another type of class for ActionScript 2.0. That's the intrinsic class. This type of class, however, is not really a class. It's more of a set of guidelines for a class. It serves only one purpose - to provide strict date type definitions for pre-existing classes.

Macromedia uses intrinsic classes to define data types for all internal objects and classes defined natively within Flash (you may have seen these in a Classes folder if you've ever dug around in your Flash MX 2004 install directory). These include objects like Array, MovieClip and Math. But when might you use them? That depends. When do you need to define just data typing for classes?

The best and probably the only situation where you personally, as a developer, would physically create an intrinsic class yourself would be if you were using ActionScript 1.0 classes within your ActionScript 2.0-based movie. ActionScript 1.0 classes, as you know, have no strict data typing associated with them. This isn't a horrible thing, but it's not a great thing either. Being the MX 2004 (or later) developer you are, you need those data type definitions to help you maintain efficiency and competency in your project. That's where intrinsic classes come into play.

Lets say you have a great class you made in ActionScript 1.0 that does everything you would ever need for this new project your working on. Only one problem, you're now working in ActionScript 2.0, not ActionScript 1.0. Ok, no problem. You can use ActionScript 1.0 with ActionScript 2.0 seamlessly easily enough. The only thing is, you won't have those amazing data type definitions for your wonderful class. Instead of re-writing your ActionScript 1.0 class completely as a new ActionScript 2.0 class, you can instead just write an intrinsic class that outlines the data types used and have it be applied to your still functional (and still wonderful) ActionScript 1.0 class as its being used within your current project.

[ intrinsic classes are type definitions for existing classes ]

All you would need to do is create a new ActionScript 2.0 class file with the name of your pre-existing class and label it as being intrinsic just as you label the class as being dynamic (it can be both if you want). Within this file, put all your property and methods with proper typing but no definitions. After all, you're avoiding re-writing your class completely, so the definitions themselves are not included.

intrinsic class className {

Here's an example that adds data typing to an ActionScript 1.0 class defined within the main Flash movie.

// in Wonderful.as
intrinsic class Wonderful {
var msg:String;
function doSomethingWonderful(allow:Boolean):Void;
}
// ActionScript 1.0 class in main Flash movie
var Wonderful = function(msg){
this.message = msg;
};
Wonderful.prototype.doSomethingWonderful = function(allow){
if (allow) {
trace(this.message +" is Wonderful!");
}
};
 
var ItsA:Wonderful = new Wonderful("Life");
ItsA.doSomethingWonderful("yes"); // error: type mismatch
ItsA.doSomethingWonderful(true); // Life is Wonderful!

Try it yourself! (zipped source)

Though the Wonderful class was created in the ActionScript 1.0 style of class definition, the compiler was still able to recognize a type mismatch when attempting to use a string as an argument for doSomethingWonderful. This is thanks to the definitions as specified in the intrinsic class in Wonderful.as.

One thing to be aware of if using intrinsic classes in this manner is that your existing class definitions (constructor functions) need to be defined with the var keyword. Otherwise an error will be generated because it assumes you're actually using the pre-existing intrinsic class and not creating a new definition. This could require some editing of older ActionScript 1.0 classes to correct this. At that point, however, it might not be such a bad idea to simply re-write it in ActionScript 2.0 altogether.

Intrinsic class definitions are also created for the new MX 2004 components. Because this new generation of components can be compiled prior to their use, it means there are no hooks for the Flash compiler to check to make sure you're using the component and its methods right. An intrinsic class gives the compiler the information it needs about the definitions contained within the component so that it can check for proper usage and data typing mismatches when your working with that component in your Flash movie. Though the intrinsic classes are not compiled within the .swc file (they're available in text format), because intrinsic classes only contain definitions without the implementation, the component author's internal code is still kept confidential.

 




SUPPORTERS:

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