Rendering from an associative array

I briefly searched the forums, and I hope i’m not bringing up an old issue but I could use a hand with some syntax on a programming concept.

The concept is to create an associative array of objects and then to use that array to create a set of DisplayObjects based on a selected index of that array.

Specifically, i’m creating “tiles” and i’m getting my data from XML. My working code is this:
[FONT=Arial][SIZE=2]
[/SIZE][/FONT]


var itemIndex:int = 0;
var collection:Array =  new Array();
  
 for (i=0; i<anXMLList.length(); i++)  {
  var tileObject:Object = {imgsrc:imgXMLList*,  caption:titleXMLList*};
  collection.push(tileObject);
 }

I have created a Tile class to handle creating the visual component. How would I use this array to create only three(3) Tiles based on the “itemIndex” ? (there are lots of tiles to create)

well firstly your collection var is a numeric array not an associative one. At least in this implementation that is how it appears to be. Secondly if you plan to use an associative array, you should probably use an Object or a Dictionary class.

As for creating a tile based on some stored information I would probably approach it like this. You could make a tile manager class that stores all the possible tile information (as you have done above). Then you could create a few convenience methods such as getTileByIndex or getTileByName. This would then provide you with an assembled Tile object to use for whatever. Or if you need to be a bit more abstract you could have those methods pass back the tile info object (like your tileObject) and then you could pass that to your Tile class in the constructor.

It sounds like a bit more work, but I think this will provide you with a) global means to access your TileManager class (assuming you use a Singleton implementation or static methods) and b) allow you to use a factory type implementation much like the data renderers you see in Flex’s list-based classes.

Thanks for clarifying!

Will this type of Array work for this purpose? It seems to do the job I need to, I’m having trouble accessing the Object’s properties from the array.

A better question might be, how do I access these properties from outside the array?


var tileObject:Object = {imgsrc:imgXMLList*, caption:titleXMLList*};
collection.push(tileObject);

OK I got this figured out.

The syntax needed to access the property of the object stored in my array is


collection[0]["imgsrc"]

// Where
collection[index][Object's property];

Thanks for your help!