Simple collection example

The following is a simple example of a component class file called MyShelf.as. This example contains a collection property along with a minimal set of imports, methods, and declarations for a component that inherits from the UIObject class.

If you import mx.utils.* in this example, the class names from mx.utils no longer need to be fully qualified. For instance, mx.utils.Collection can be written as Collection.

import mx.utils.*;
// standard class declaration
class MyShelf extends mx.core.UIObject
{
// required variables for all classes
    static var symbolName:String = "MyShelf";
    static var symbolOwner:Object = Object(MyShelf);
    var className:String = "MyShelf";

// the Collection metadata tag and attributes
    [Collection(variable="myCompactDiscs",name="My Compact Discs",collectionClass="mx.utils.CollectionImpl", collectionItem="CompactDisc", identifier="Title")]
    
// get and set methods for the collection
    public function get MyCompactDiscs():mx.utils.Collection 
    { 
        return myCompactDiscs; 
    }     
    public function set MyCompactDiscs(myCDs:mx.utils.Collection):Void
    {
        myCompactDiscs = myCDs;
    }

    // private class member
    private var myCompactDiscs:mx.utils.Collection;

// You must code a reference to the collection item class
// to force the compiler to include it as a dependency 
// within the SWC
    private var collItem:CompactDisc;

// You must code a reference to the mx.utils.CollectionImpl class
// to force the compiler to include it as a dependency 
// within the SWC
    private var coll:mx.utils.CollectionImpl;

    // required methods for all classes
    function init(Void):Void {
        super.init();
    }
    function size(Void):Void {
        super.size();
    }    
}

To create a FLA file to accompany this class for testing purposes:

  1. In Flash, select File > New and create a Flash document.
  2. Select Insert > New Symbol. Give it the name, linkage identifier, and AS 2.0 class name MyShelf.
  3. Deselect Export in First Frame and click OK.
  4. Select the MyShelf symbol in the library and choose Component Definition from the Library options menu. Enter the ActionScript 2.0 class name MyShelf.
  5. Select Window > Common Libraries > Classes, and drag UtilClasses to the library of MyShelf.fla.
  6. In the MyShelf symbol's Timeline, name one layer Assets. Create another layer and name it Actions.
  7. Place a stop() function on Frame 1 in the Actions layer.
  8. Select Frame 2 in the Assets layer and select Insert > Timeline > Keyframe.
  9. Open StandardComponents.fla from the Configuration/ComponentFLA folder, and drag an instance of UIObject to the Stage of MyShelf in Frame 2 of the Assets layer.

    You must include UIObject in the component's FLA file because, as you can see in the above class file, MyShelf extends UIObject.

  10. In Frame 1 of the Assets layer, draw a shelf.

    This can be a simple rectangle; it's just a visual representation of the MyShelf component to use for learning purposes.

  11. Select the MyShelf movie clip in the library, and select Convert to Compiled Clip.

    This allows you to drag the MyShelf SWF file (the compiled clip that's added to the library) into the MyShelf.fla file to test the component. Whenever you recompile the component, a Resolve Library Conflict dialog box appears, because an older version of the component already exists in the library. Choose to replace existing items.

    NOTE

     

    You should have already created the CompactDisc class; otherwise, you'll get compiler errors when converting to a compiled clip.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00002529.html