ThinkGeek - Cool Stuff for Geeks and Technophiles

January 11, 2004

AS2 and prototypes

How could we make a method available to all movieclip instances?. It's easy, adding it to the movieclip prototype. But is it that easy when we code in AS2?

Now, we can't modify the movieclip class, we are only suppossed to extend it, addingh methods to its subclasses. ¿Sure?.

OOP theory says that, to add a method to the movieclip class, we should write something like this:

class net.designnation.exp.TestClass extends MovieClip{
 
     function TestClass(){
          trace("constructor");
      }
 
     //This one is the new method  we want to add
     private function methodAS1(){
          trace("I'm the new method!");
      }
}

So this class inherits all the movieclip methods, and has a new method called methodAS1. But that method belongs only to TestClass. So, if we write a new class that inherits movieclip, we should define it's methodAS1 method.

Some of you, maybe have thought yet: we need many different classes implementing the same method, hmmmm, what about defining an interface?

interface net.designnation.exp.MCInterface{
     public function methodAS1():Void;
}

So, our class looks like this now:

import net.designnation.exp.*;
class net.designnation.exp.TestClass extends MovieClip implements MCInterface{
 
     function TestClass(){
          trace("TestClass constructor");
      }
 
     //Here we must implement method
     //that belongs to the interface
     private function methodAS1(){
          trace("defined in the interface");
      }
}

Well, is our problem solved? Not completely. Now we are forced to implement a given method in all the classes that implement the interface. Just to remind you (and me): we want to implement a method only once, and make it accesible to all our classes.

Maybe the most elegant solutions will be to implement a class that inherits from movieclip, and implement our methodAS1 in that class. Then, we'll make all our classes inherit from that new class. But, you know, this is flash, let´s have some fun!.

So, in the first frame of our fla, we could write:

MovieClip.prototype.methodAS1 = function(){
    trace("Done!. I'm the right method!");
};

And our class will look like this:

class net.designnation.exp.TestClass extends MovieClip{
 	
     function TestClass(){
          trace("constructor");
      }
 	
     private function methodAS2(){
          trace("I'm the class method");
      };
 	
     public function onRollOver(){
          //Runs the superclass method
          super.metodoAS1();
          methodAS2();
      };
}

To see this example in action, I've created a MovieClip drawing a circle inside it, that I'll use as a button. It's linkage name is ”MyClip” and the actionscript 2.0 class is “net.designnation.TestClass”.

So, back to the fla's first frame:

MovieClip.prototype.methodAS1 = function(){
    trace("Done!. I'm the right method!");
};

//Attachs the movie, and instanciates TestClass
this.attachMovie("MiClip", "instancia", 1);

So, what happens when moving the mouse over the clip?. Right!. We got it!. Well, not at all. We are running one of the superclass (MovieClip) methods, so what happens if we want to run another method of TestClass from its superclass?

If we change TestClass:

class net.designnation.exp.TestClass extends MovieClip{
 	
     function TestClass(){
          trace("constructor");
      }
 	
     private function metodoAS2(arg){
          trace("I'm the class method " + arg);
      };
 	
     public function onRollOver(){
          //Runs the superclass method
          super.methodAS1(this);
          //methodAS2();
      };
}

And if we change the method definition:

MovieClip.prototype.methodAS1 = function(ref){
    trace("Done!. I'm the right method!");
    ref.methodAS2("_width " + this._width);
};

So, we've called a method of the superclass, passing it a reference to the class that called it. So, now, the superclass knows who's called it.

Congratulations if you didn't felt asleep.

Posted by Cesar Tardaguila Date: January 11, 2004 09:50 PM | TrackBack
Comments