
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
324
|
Chapter 13: MovieClip Subclasses
Note that the preceding cast is an (unsafe) downcast. If we were being more cau-
tious, we’d use:
var av:Avatar;
var temp_mc:MovieClip = someMovieClip.attachMovie("AvatarSymbol",
"avatar", 0);
// Verify that the cast will succeed at runtime before attempting it.
if (temp_mc instanceof Avatar) {
av = Avatar(temp_mc);
} else {
trace("Warning: could not cast temp_mc to type Avatar.")
}
For full information on casting, see Chapter 3.
The instance-creation code discussed in this section can be used in the
AvatarDemo.fla, in any class loaded by AvatarDemo.fla, or, more generally, in any
class that has access to both the Avatar class and the AvatarSymbol movie clip
symbol. When a MovieClip subclass is reused across many projects, consider com-
piling it as a component, which includes both the class and the symbol in a single
file. For details on creating components, see Help
➝ Using Components ➝ Creat-
ing Components in Flash’s online Help.
Initializing Avatar Instances
To initialize our new Avatar instance, we invoke init( ) on it, as follows:
av.init( );
Creating Avatar instances with the attachMovie( ) syntax can be ungainly because we
have to specify the AvatarSymbol and the name of that symbol’s physical incarna-
tion (“avatar”) in the call. Furthermore,