
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
326
|
Chapter 13: MovieClip Subclasses
public function init (x:Number, y:Number):Void {
setState(Avatar.HAPPY);
this._x = x;
this._y = y;
}
public function setState(newState:Number):Void {
switch (newState) {
case Avatar.HAPPY:
this.gotoAndStop("HAPPY");
break;
case Avatar.SAD:
this.gotoAndStop("SAD");
break;
case Avatar.IDLE:
this.gotoAndStop("IDLE");
break;
}
}
}
Avatar: The Composition Version
Now that we’ve seen how to implement Avatar as a MovieClip subclass, let’s con-
sider an alternative using object composition. This time, the Avatar class does not
inherit from MovieClip. Instead, it creates an instance of AvatarSymbol and stores it
in an instance property,
av_mc. (Well, technically, the ActionScript object representa-
tion of that instance is stored in
av_mc, but here is an example in which making a dis-
tinction between the symbol instance and the ActionScript object would make the
discussion awkward, albeit more technically accurate. My editor tells me to “Avoid
precision where it merely sacrifices clarity.” So I will here and elsewhere in this book.)
Example 13-2 shows a revised version of the Avatar class. This version defines a con-
structor because, now that Avatar is no longer a subclass of MovieClip, instances are
instantiated with new. The AvatarSymbol clip instance ...