Ultrashock Tutorials > Flash MX 2004 > ActionScript 2.0  
 
by Dave Yang, quantumwave.com - swfoo.com
Download Source Files 
 
ActionScript 2.0
 

01. Introduction
02. A little bit of OOP in ActionScript 1.0 
03. What's new in ActionScript 2.0?
04. New keywords and features for OOP
05. Scope and "this"
06. Private or protected?
07. Dynamic vs Static classes

08 Inheritance.
09. Overriding, Overwriting or Overloading?

10. Importing external class files
11. Interface
12. What is missing in ActionScript 2.0?
13. Conclusion

- discuss this tutorial -

02. A little bit of OOP in ActionScript 1.0

Before we move on to ActionScript 2.0, let's review OOP in ActionScript 1.0. This section is especially useful for readers who are not familiar with OOP in Flash 5 and Flash MX. Experienced developers may skip ahead to What's new in ActionScript 2.0?

Although ActionScript 1.0 is not a true OOP language, developers have been programming object-oriented applications with it for some time. Everything in AS1 depends on the prototype chain -- the underlying structure of how objects are related. As a result, OOP in ActionScript 1.0 requires a knowledge of this prototype chain (or more specifically, the prototype keyword).

ActionScript 1.0 classes are implemented just like regular functions. Methods (and sometimes properties) are attached to the prototype object of the class. For example:

// Wizard class
function Wizard() {
}
// A method called help() is attached to the prototype 
// object of the Wizard function
Wizard.prototype.help = function() { };
When Flash looks for a property or method of an object and can't find it inside the object, it searches along the prototype chain of the object. If this help() method is placed inside the class instead, Flash creates a new copy of the method for every instance of the Wizard class:
// This creates a new copy of the function help() in every instance
function Wizard() {
   this.help = function() {
   };
}
To Java, C++ or C# programmers, this syntax looks more familiar, with the method inside the class. However, in order to save memory (by sharing the same code at one location), methods (and shared properties) should be placed in the prototype object of the class.

In the following example, if we have two methods of the same name, one attached to the prototype object and one inside the class, Flash will reach the internal method first (it overrides the prototype method):

// AS1_OOP_01.fla
function TestClass() {
   this.method = function() {
      trace("Internal method");
   };
this.prop = ">>> Internal prop"; }
// Attach a method to the prototype object of the class
TestClass.prototype.method = function() {
   trace("Prototype method");
};
TestClass.prototype.prop = ">>> Prototype prop";
// Create an instance of the TestClass class
var w = new TestClass();
// Internal method is located before the prototype method
w.method();
// Replace the Internal method
w.method = function() {
   trace("New method");
};
w.method();
// Delete the Internal method
delete w.method;
// The only method remaining is the prototype method
w.method();
// Test the properties
trace(w.prop);
w.prop = ">>> New prop";
trace(w.prop);
delete w.prop;
trace(w.prop);
         

The output of this example is shown below.

As you can see, OOP in ActionScript 1.0 can be rather confusing for beginners. Knowing where to put code is important because undesirable results may occur that can lead to hair lost debugging the code. As they say in commercials: But wait, there's more...

One of the important concepts in OOP is Inheritance. Basically you get whatever your parents and ancestors have, for free. If you don't like whatever they pass on to you, you can change them to your liking. Inheritance in ActionScript 1.0 is officially coded like this:

function SuperClass() {
// SuperClass constructor
}
SuperClass.prototype.parentMethod = function() {
};
// A subclass that inherits from SuperClass
function SubClass() {
   super();
}
// Set up the inheritance relationship: SubClass inherits from SuperClass
SubClass.prototype = new SuperClass();
SubClass.prototype.childMethod = function() { };
There are a couple of issues with this set up:

The syntax is non-standard: How does over-writing the prototype object with an instance of the superclass establish inheritance? No other OOP language works like this. In effect, this is what SuperClass is saying to the SubClass: "If you want to inherit from me, first create a new instance of me, and inherit from it, don't inherit from me directly." What happens is, there are all these nameless instances of superclasses hanging around, taking up memory, just to define the parent/child relationships.
Anything placed in a class's prototype object is wiped out, so make sure you don't have anything attached to it before inheriting another class.
While setting up the relationship between the SubClass and SuperClass, the new statement invokes the SuperClass constructor. This limits what can be executed inside the constructor of superclasses. For examples, if you are incrementing a global value in the constructor, or creating and displaying movieclips, you'll end up with extra values or unwanted movieclips.
There are workaround solutions, for example, using __proto__ to set up the relationships directly, but it gets even more confusing for programmers who are new to ActionScript. The fact is, OOP in ActionScript 1.0 takes more effort than it should, because the language wasn't really designed for true object-oriented programming.

The benefits of OOP are numerous (such as more maintainable, scalable and reusable code). If we can achieve the benefits without all that mess, it is worth every bit (and byte) of the effort! The ECMA-262 Edition 4 proposal addresses most of these issues, and ActionScript 2.0 takes advantage of this proposal by introducing a new OOP model and concepts.


- discuss this tutorial -
 
©2003 Ultrashock.com - All rights reserved