Introduction. Associative Arrays.
An associative array uses keys to organize stored values. That means that you can use strings instead of indexes, so that the next statement becomes perfectly legal.
buttons['home'] = new Button(); //buttons is an associative array contentSprites[1] = new Sprite(); //contentSprites is an numeric indexed array |
Working Example. Dictionary Class.
Although the principle behind the associative array is very simple, we usually use the numeric indexed array. However there are many scenarios in which an associative array would do the job much easier, more organized and nonetheless less cryptic for others.
But what I would really like to talk about is the Dictionary Class. The Dictionary Class is very similar indeed with a associative array but it has the great feature that it can use as indexes any objects from Movieclips to Tweens (usually the object passed as index is an instance of the Object Class, but theoretic it could be anything ).
Next is an example that shows you how and most of all why you should use the Dictionary Class in certain scenarios.
package { import fl.controls.Button; import flash.display.Sprite; import flash.ui.Mouse; import flash.utils.Dictionary; import flash.events.MouseEvent; public class dictonaryEx extends Sprite { private const buttonsObjects:Array = new Array( { label:"Home", listener:$btn1, no:0 }, { label:"Products", listener:$btn2, no:1 }, { label:"Clients", listener:$btn3, no:2 }, { label:"Contact", listener:$btn4 , no:3 } ); public function dictonaryEx() { var buttons:Dictionary = new Dictionary(); for each(var obj:Object in buttonsObjects) { buttons[obj] = new Button(); buttons[obj].label = obj.label; buttons[obj].addEventListener(MouseEvent.CLICK, obj.listener); buttons[obj].x = buttons[obj].width * obj.no; addChild(buttons[obj]); } } private function $btn1(evt:MouseEvent):void { trace(evt.currentTarget.label+' pressed')//output "Home pressed" } private function $btn2(evt:MouseEvent):void { trace(evt.currentTarget.label+' pressed')//output "Products pressed" } private function $btn3(evt:MouseEvent):void { trace(evt.currentTarget.label+' pressed')//output "Clients pressed" } private function $btn4(evt:MouseEvent):void { trace(evt.currentTarget.label+' pressed')//output "Contact pressed" } } } |
Closer look to the example.
First, we can look at the buttonsObjects array as a model from the MVC’s terminology, a entity that holds all the data for creating visual buttons. Depending on how complex the buttons are the buttonsObjects array can hold more complex objects. For this example, I choose to use only three properties label, listener, no (the label is the text that will show up on the button, the listener is the function that will be called when the mouse click ocurs and the no is the index of the button counting from left to right). In the dictonaryEx constructor we create a visual button using the Button Class for each object in the buttonsObjects array.
I think Dictionary Class is a great utility first because it does the same thing as an indexed array but with less code and in a way that others will understand better what you did. I certainly use it, but not abuse it.
If you have any questions please leave a comment. Cheers mates!