User Guide

Accessing collection information programmatically 193
Accessing collection items at runtime in a
Flash application
If a Flash application uses a component that has a collection property, you can access the
collection property at runtime. This example adds several items to a collection property using
the Values dialog box and displays them at runtime using the Collection and Iterator APIs.
To access collection items at runtime:
1. Open the MyShelf.fla file that you created earlier.
See “Simple collection example” on page 189.
This example builds on the MyShelf component and CompactDisc collection.
2. Open the Library panel, drag the component onto the Stage, and give it an instance name.
This example uses the instance name myShelf.
3. Select the component, open the Component inspector, and display the Parameters tab.
Click the line that contains the collection property, and click the magnifying glass to the
right of the line. Flash displays the Values dialog box.
4. Use the Values dialog box to enter values into the collection property.
5. With the component selected on the Stage, open the Actions panel and enter the following
code (which must be attached to the component):
onClipEvent (mouseDown) {
import mx.utils.Collection;
import mx.utils.Iterator;
var myColl:mx.utils.Collection;
myColl = _parent.myShelf.MyCompactDiscs;
var itr:mx.utils.Iterator = myColl.getIterator();
while (itr.hasNext()) {
var cd:CompactDisc = CompactDisc(itr.next());
var title:String = cd.Title;
var artist:String = cd.Artist;
trace("Title: " + title + " Artist: " + artist);
}
}
To access a collection, use the syntax componentName.collectionVariable; to access an
iterator and step through the collection items, use
componentName.collectionVariable.getIterator().
6. Select Control > Test Movie and click the shelf to see the collection data in the
Output panel.