CURRENT PROJECTS
loading
CATEGORIES AND POSTS
loading
overset
DEVELOPMENT LOG FOR JIM PALMER
Posted 07/13/2006 in flash


So you're in the process of building a custom component and there's still a black area of how to deal with more complex data types than Number/String/Array/Object... Then you find a few online tutorials on how to configure a "Collection" as a configurable parameter for your custom component... But what to do with that data after it's been instantiated.

For instance, say we have a custom class that extends MovieClip and we are attempting to make this a custom component. We want a more complex "Collection" datatype as one of the configurable parameters. For those who don't know what a "Collection" in Flash it can be simply described as an Array of Object's.

Say we utilize the following [Collection] tag in our class file to define a custom "Collection" parameter:
[Collection (name="options", variable="optionParms", collectionClass="mx.utils.CollectionImpl", collectionItem="Option", identifier="value")]
public var optionParms:mx.utils.Collection;
public var optionsRow:mx.utils.CollectionImpl;
The variable tag defines the end variable the "Collection" will be stored in also known as the container. The collectionClass tag defines the actual class definition with which the "Collection" variable will be instantiated as. As far as I understand the mx.utils.CollectionImpl will only with in this [Collection] parameter with Flash. The collectionItem tag defines the instantiated container object/class for each "Item" in the "Collection". In this case "Option" is a simple class as follows:
class Option {
	[Inspectable(type="String", defaultValue="value")]
	var value:String;
	[Inspectable(type="String", defaultValue="option")]
	var option:String;
};
The identifier tag is the index/key of the "Item" object that will appear as the identifier of each "Item" in the "Collection". In other words, I used the "value" as my identifier which means in the parameter edit screen in the Flash IDE each "Item" in the "Collection" will display that item's "value" value (heh) as the key in the collection for said item. This sounds confusing as heck, but just set the identifier as one of the variable names in the collectionItem class and you'll see how it affects the parameter editing.

Now comes the interesting part... I have a component configured, I drop it onto the stage, I'm able to edit the collection parameter and now I want to access those values via actionscript. Basically the instantiated component, as it sits on the stage, sets whatever collection parameters it has into mx.utils.CollectionImpl objects. If you take a look at the CollectionImpl.as file that comes stock with the Flash IDE, you'll notice that the data is encapsulated in an Iterator. Iterators are fantastic, but you've probably never used one before in Flash. It's worth just looking at the mx.utils.Iterator interface and the mx.utils.IteratorImpl class file to get a handle of what it does. In short, I use the following code to access the configured collection parameter:
// this.optionParms is the instance name assigned to the component on the stage
var collectParms:Iterator = this.optionParms.getIterator();
while (collectParms.hasNext()) {
	this.options.push(collectParms.next());
	// tmpObj is not scrictly cast because the iterator can contain any datatype
	var tmpObj = collectParms.next();
	// but let us just output the two variables present in the expected collection item
	trace("value: " + tmpObj.value + " & option: " + tmpObj.option);
}
comments
loading
new comment
NAME
EMAIL ME ON UPDATES
EMAIL (hidden)
URL
MESSAGE TAGS ALLOWED: <code> <a> <pre class="code [tab4|tabX|inline|bash]"> <br>
PREVIEW COMMENT
TURING TEST
gravatar