Table of Contents

14.3 Assigning the MovieClip Superclass

We've created our Ball class constructor, and we have seen how to instantiate Ball objects with attachMovie( ). Now we have to make our Ball class a subclass of the MovieClip class. As we saw in Chapter 12, we declare a class's superclass like this:

SubClass.prototype = new SuperClass( );

Hence, it comes as no surprise that we declare MovieClip as Ball's superclass like this:

/*
 * Set MovieClip as Ball's superclass.
 */
org.moock.Ball.prototype = new MovieClip( );

However, although we've defined Ball as a subclass of MovieClip, we still need to tell the interpreter to execute the Ball constructor when we create a new instance of our exported ballSymbol from the Library. To associate an exported symbol with the desired class constructor, we use Object.registerClass( ). Here we associate ballSymbol with the Ball class:

/*
 * Associate the Library's ballSymbol with the Ball class.
 */
Object.registerClass("ballSymbol", org.moock.Ball);

In plain English, this tells the interpreter, "when a ballSymbol instance is created, invoke the org.moock.Ball( ) constructor, with the this keyword pointing to the new ballSymbol instance."

The new MovieClip( ) statement only establishes a class as a subclass of the MovieClip class (i.e., it merely establishes inheritance). It cannot be used to create new movie clips for use in a movie! Use attachMovie( ), duplicateMovieClip( ), or createEmptyMovieClip( ) to create new instances of movie clips or movie clip subclasses, such as Ball.

For more information on Object.registerClass( ), see the ActionScript Language Reference.


Table of Contents