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 and displays them at runtime using the Collection and Iterator APIs.
To access collection items at runtime:
Open the MyShelf.fla file that you created earlier.
1.
See
"Simple collection example" on page
This example builds on the MyShelf component and CompactDisc collection.
Open the Library panel, drag the component onto the Stage, and give it an instance name.
2.
This example uses the instance name myShelf.
Select the component, open the Component inspector, and display the Parameters tab. Click
3.
the line that contains the collection property, and click the magnifying glass to the right of the
line. Flash displays the Values dialog box.
Use the Values dialog box to enter values into the collection property.
4.
With the component selected on the Stage, open the Actions panel and enter the following code
5.
(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
iterator and step through the collection items, use
componentName
Select Control > Test Movie and click the shelf to see the collection data in the Output panel.
6.
Exporting components that have collections to SWC files
When you distribute a component that has a collection, the SWC file must contain the following
dependent files:
•
Collection interface
•
Collection implementation class
•
Collection item class
•
Iterator interface
964
Appendix: Collection Properties
componentName
.
collectionVariable
960.
.
collectionVariable
.
.
getIterator()
; to access an
Need help?
Do you have a question about the FLASH MX 2004-USING COMPONENTS and is the answer not in the manual?