AS2 OOP: Class Structure
         by senocular  

New Class Syntax
So classes are now defined in their own separate text file. What goes in these files? Why the class of course! What you have in these files, or rather, what you'll put in these files, is one block of code to represent the entire definition of the class you intend to create. This block takes on a general form of

class className { /* class definition block */ }

The class keyword is a new keyword for ActionScript 2.0 that indicates the code block that follows is a class definition. When this particular definition is saved, it gets saved under the name of className.as. Note that the .as file extension is not part of the name of the class within the actual definition.

The class definition within the code block contains everything associated with that class. It contains properties and methods, both shared and static, as well as the constructor itself. Yes, that's right, the constructor function is defined within a class block.

With ActionScript 1.0, classes were defined by their constructor function. For example, here is the start of a simple Point class in ActionScript 1.0.

/* Actionscript 1.0 */
Point = function(x,y) {
this.x = x;
this.y = y;
}

Just by defining a function, it can be used as a constructor and therefore also represents the definition of a class. With ActionScript 2.0, a class code block is itself not a constructor, however. Its just an indicator that what follows is the parts needed to define a class. Because of that, you would need to define your constructor separately within that block.

class Point {
function Point(){
// constructor
}
// more class definition...
}

When defining constructors for classes, they need to be functions that have the same name as the class itself. This function is defined using the form function [className]() and not [className] = function() which you may be accustomed to in ActionScript 1.0. The constructor and its methods each use function [className]() format in their definition, so be sure to get used to defining functions in that manner. If you don't create a constructor function for your class, Flash will create one for you. This automatic constructor would be equivalent to a function with no body.

Classes, as you are well aware of, aren't all constructors. They need more than that to serve any real functionality...

 

 




SUPPORTERS:

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