Just like a regular class, a MovieClip subclass can itself be extended by another subclass. When a parent class is defined outside of its corresponding movie clip symbol (as in the very first Ball example in this chapter), its child subclass can be defined by:
Setting the child's prototype to a new parent instance
Registering the child class with its symbol via Object.registerClass( )
Remember also to invoke the parent's constructor function from the child constructor. For example, the following code shows how to create a subclass, Baseball, of our existing class, Ball:
// Baseball class constructor
org.moock.Baseball = function ( ) {
// ... init code ...
// Invoke Ball class constructor
super( );
}
// Set Baseball's superclass to Ball
org.moock.Baseball.prototype = new org.moock.Ball( );
// Associate the Baseball class with its own Library symbol
Object.registerClass("baseballSymbol", org.moock.Baseball);
However, when both the subclass (Ball) and the sub-subclass (Baseball) are defined inside a movie clip symbol's #initclip block, we must ensure that the subclass is initialized before any of the sub-subclasses. To do so, we use the order parameter of the #initclip pragma, which is a nonnegative integer specifying the execution order of a particular #initclip block, relative to all other #initclip blocks in the movie. Those #initclip blocks with lower order numbers are always executed before those with higher ones. All #initclip blocks without a specified order are executed before those with a specified order.
For example, to create a Baseball class inside the movie clip baseballSymbol, we must change the first line of our Ball class code to:
#initclip 0
Then we can create our Baseball class as follows:
// Use a higher #initclip order than Ball uses, guaranteeing that Ball
// will be available before this code runs.
#initclip 1
// = = = = = = = = = = = = = = = = = = = = = = = = = =
// Create namespace in which to store classes
// = = = = = = = = = = = = = = = = = = = = = = = = = =
if (_global.org = = undefined) {
_global.org = new Object( );
}
if (_global.org.moock = = undefined) {
_global.org.moock = new Object( );
}
// = = = = = = = = = = = = = = = = = = = = = = = = = =
// Create the Baseball class
// = = = = = = = = = = = = = = = = = = = = = = = = = =
/*
* Baseball Class. Extends Ball.
* Version: 1.0.0
* Desc: A movie clip subclass for depicting baseballs
*
* Constructor Params:
* signature -The ball's signature
*
* Methods:
* setSignature( ) -Set the signature on the ball
*/
/*
* Class Constructor. Parameters are passed by attachMovie( )'s initObj.
*/
org.moock.Baseball = function ( ) {
// Create Instance properties.
this.sig = null;
// Initialize instance before calling Ball constructor. (Otherwise,
// Ball will delete the params object before we get a chance to use it.)
this.setSignature(this.params.signature);
// Invoke Ball class constructor.
super( );
// No need to delete params object, because the Ball constructor does that.
};
/*
* Set Ball as Baseball's superclass.
*/
org.moock.Baseball.prototype = new org.moock.Ball( );
/*
* Associate the Library's baseballSymbol with the Baseball class
*/
Object.registerClass("baseballSymbol", org.moock.Baseball);
/*
* Instance Methods
*/
/*
* Method: Ball.setSignature( )
* Desc: Sets the ball's signature
*
* Params:
* newSig -The ball's new signature
*/
org.moock.Ball.prototype.setSignature = function (newSig) {
this.sig = newSig;
};
#endinitclip
Note that multiple #initclip blocks can share a single order position. For example, if we create three subclasses of Ball, we can set all of their #initclip order parameters to 1. All that matters is that the Ball's #initclip order is lower than all its subclasses.