View Single Post
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator 17 Creative Assets 2006-11-04 #4 Old  
003 - Add Classes to MovieClips at Runtime
 
Normally you would link a class to a library symbol if you wanted to add a class to a MovieClip. You can however take advantage of __proto__ if you want to add a class to a movie clip at runtime.

ActionScript Code:
  1. myMovieClip.__proto__ = new MyClass();
MyClass should extend MovieClip in the usual way.

The only downside to this is that physical changes to the movie clip don't seem possible from the class constructor. For example, this._x = 0 in the following class has no effect on the movie clip.

ActionScript Code:
  1. class MyClass extends MovieClip {
  2.  
  3.     public function MyClass() {
  4.         this.onRollOver = rollOverHandler;
  5.         this.onRollOut  = rollOutHandler;
  6.  
  7.         this._x = 0;
  8.     }
  9.  
  10.     private function rollOverHandler():Void {
  11.         this._xscale *= 2;
  12.         this._yscale *= 2;
  13.     }
  14.  
  15.     private function rollOutHandler():Void {
  16.         this._xscale /= 2;
  17.         this._yscale /= 2;
  18.     }
  19.  
  20. }