13.5 A Note on MovieClip Sub-subclasses

 <  Day Day Up  >  

Nothing officially prevents a MovieClip subclass from being subclassed. For example, hearkening back to the Avatar class, if we wanted to create an Avatar whose mood changed randomly , we could create a RandomAvatar class that inherits from Avatar . However, the RandomAvatar class would need to be associated with its own library movie clip symbol because, as we learned earlier, a movie clip symbol can specify only a single AS 2.0 Class association. Hence, while the RandomAvatar class would inherit the behavior of the Avatar class, it would not inherit Avatar 's physical movie clip symbol. Hence, to create the RandomAvatar class, we'd have to create a completely separate symbol, RandomAvatarSymbol , that is a duplicate of AvatarSymbol .

Duplicating a symbol merely for the sake of subclassing is neither practical nor wise in most cases. The better approach is (yet again) to use composition. When a class uses a movie clip symbol via composition, the class can easily be subclassed, allowing reuse both of its code and of the movie clip symbol associated via composition. Example 13-3 shows how we could create the RandomAvatar class as a subclass of the Avatar class shown earlier in Example 13-2. Neither Avatar nor RandomAvatar are MovieClip subclasses. We use composition to associate them with a movie clip symbol.

Example 13-3. RandomAvatar, an Avatar subclass
 class RandomAvatar extends Avatar {   private var randomInt:Number;   public function RandomAvatar (name:String, target:MovieClip,                            depth:Number, x:Number, y:Number) {     super(name, target, depth, x, y);     startRandom( );   }   public function startRandom ( ):Void {     randomInt = setInterval (function (av:RandomAvatar):Void {       var r:Number = Math.floor(Math.random( ) * 3);       av.setState(r);     }, 500, this);   }   public function stopRandom ( ):Void {     clearInterval(randomInt);   } } 

For comparison, the example files for the inheritance and composition versions of the RandomAvatar class are posted at http:// moock .org/eas2/examples.

 <  Day Day Up  >  


Essential ActionScript 2.0
Essential ActionScript 2.0
ISBN: 0596006527
EAN: 2147483647
Year: 2004
Pages: 177
Authors: Colin Moock

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net