AS2 OOP: Class Structure
         by senocular  

Dynamic Classes
Everything seems much more constrictive with ActionScript 2.0. Well, this is because it is. It's not a bad thing, though. It helps you adhere to your own intentions and promotes better practices for you as an OOP programmer. One more of these constraints is the fact that, by default, classes cannot have properties or methods created for them if they are not first declared within the class definition. This means if sometime in the middle of a movie you all of a sudden up and decide your class instance needs another property, it's not going to get it. At least this is the case for normally defined classes.

Enter dynamic - the keyword that lifts this restriction. It lets you add any number of properties or methods to your class instance whenever you want whether or not they were declared in the class definition, just like you could with ActionScript 1.0 just by adding "dynamic" before the class keyword.

dynamic class className {

Note you can only add public properties outside of the class definition. Private properties must be declared in the class.

Lets take a look at a quick example of two classes, one ordinary run of the mill class, the other dynamic.

// in ClosedSet.as
class ClosedSet {
var actor:String;
var director:String;
var cameraMan:String;
 
function ClosedSet(act:String, dir:String, cam:String){
actor = act;
director = dir;
cameraMan = cam;
}
}
// in OpenSet.as
dynamic class OpenSet {
var actor:String;
var director:String;
var cameraMan:String;
 
function OpenSet(act:String, dir:String, cam:String){
actor = act;
director = dir;
cameraMan = cam;
}
}
// in Flash movie
var closed:ClosedSet = new ClosedSet("Joe", "Jeff", "John");
var open:OpenSet = new OpenSet("Albert", "Carmen", "Saul");
 
closed.bestBoy = "Carl"; // error: property doesn't exist
open.bestBoy = "Carl"; // (works ok)
trace(open.bestBoy); // Carl

Because the ClosedSet instance, closed, is not dynamic, an error occurs when attempting to give it an undeclared variable. The bestBoy property was not previously defined in the ClosedSet class therefore it cannot be added. With OpenSet, however, which is dynamic, the open instance added the bestBoy property with no hassles.

When working with ActionScript 2.0, you need to realize that internal objects and core classes follow the same rules as those governing your custom classes. This includes their use (or lack thereof) of dynamic. In fact, now, only a few of Flash's own core classes are defined as being dynamic. These include the following.

• Array
• ContextMenu
• ContextMenuItem
• Function
• MovieClip
• NetConnection
• SharedObject
• TextField

All other internal Flash objects cannot have properties dynamically added to them. Math, a core Flash object, for example, cannot have any custom mathematical functions of your own added to it (something which was commonly done in ActionScript 1.0). Instead, you might opt to make your own Math-type object for these custom functions. Something as simple as Math2 perhaps.

class Math2 {
static function randRange(low:Number, high:Number):Number {
return low + Math.floor(Math.random()*(high-low+1));
}
}

It's depressing, I know. It's ultimately a good thing. This keeps your custom methods separated from Flash's own internal ones. Nevertheless, there is a workaround which we'll discuss later on.

 

 




SUPPORTERS:

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