14.4 Packaging Subclass Code and Library Symbols Together

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

With our superclass assigned, we've created a fully functioning MovieClip subclass, Ball. We can add methods and properties to it via Ball.prototype, just as we would with any class (as shown in Example 14-1). However, suppose we wanted to share our Ball class with another developer. We might sensibly put our class in an external .as file and tell the developer to #include the file. But we'd also have to tell her to make a Library symbol for the class and export that symbol as "ballSymbol". If the developer doesn't export the symbol, or if she exports it with the wrong linkage identifier, our class won't work.

Maintaining both a class file and an accompanying movie clip symbol can be inconvenient and error prone. To make a MovieClip subclass more self-contained, we can place our code inside the movie clip symbol itself, between the #initclip and #endinitclip pragmas (creating a so-called #initclip block).

For example, to package our Ball class inside our ballSymbol movie clip, we place the following code on frame 1 of the ballSymbol's timeline. (The function body code and some comments have been omitted for brevity).

#initclip // =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  = // 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 Ball class // =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  =  = // Class Constructor org.moock.Ball = function ( ) { ... }; // Set MovieClip as Ball's superclass org.moock.Ball.prototype = new MovieClip( ); // Associate the Library's ballSymbol with the Ball class Object.registerClass("ballSymbol", org.moock.Ball); // Method: Ball.setSize( ) org.moock.Ball.prototype.setSize = function (newRadius) { ... }; // Method: Ball.setPosition( ) org.moock.Ball.prototype.setPosition = function (x, y) { ... }; // Method: Ball.setColor( ) org.moock.Ball.prototype.setColor = function (newColor) { ... }; // Method: Ball.move( ) org.moock.Ball.prototype.move = function ( ) { ... }; #endinitclip

An #initclip block can appear only on frame 1 of a movie clip timeline. If the code is placed on the main timeline or outside of frame 1 of a movie clip timeline, it generates a compile-time error. Code within the block is executed once, and only once, per movie clip symbol, just before the first instance of the symbol is created. By defining our Ball class inside the ballSymbol's #initclip block, we guarantee that the Ball class constructor will be available before the first instance of ballSymbol appears. Methods and properties defined by Ball will be available immediately to each new Ball instance.

With our Ball class stored in our ballSymbol's #initclip block, we can easily move the entire package between movies by dragging the ballSymbol from one .fla file's Library to another. Without any more effort, the destination .fla file can then attach and use instances of ballSymbol.

For more on #initclip, see the 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