14.3 Assigning the MovieClip Superclass

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 14.  Movie Clip Subclasses and Components

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.

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

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