How to Get More Power Out of the Dictionary Class in AS3

One great nugget that many noobs to AS3 don’t know about or don’t utilize is the Dictionary Class. It is one class that I have found so useful when dealing with stored data and accessing it for future use in an application. With proper use, you can easily reference objects and/or data.

What is the Dictionary Class in AS3?

The Adobe Actionscript documentation states: The Dictionary class lets you create a dynamic collection of properties, which uses strict equality (===) for key comparison. I suggest reading this archived post by Grant Skinner that gives some good insight on the Dictionary Class. A great thing about the Dictionary Class is that any type of object can be used as a key for the Dictionary as well as its associated value: String, MovieClip, Sprite, Array, XML, etc. This is highly useful when you need to store information for future use and tie it to a specific object with a “key”.

Why Use the Dictionary Class?

Have you ever wanted to add a property to a static class such as Sprite? You will get the following error:

1119: Access of possibly undefined property --myProperty-- through a reference with static type flash.display:Sprite.

You can only add properties on dynamic classes. There are a few ways around this:

  1. Use a MovieClip instead of a Sprite
  2. Create a custom Class with public properties instantiated
  3. Use the Dictionary Class

When you use a Dictionary to reference a property or what have you, the lookup is as simple as referencing the Dictionary with the associate Sprite “key” as seen in the following example:

var spriteProperties:Dictionary = new Dictionary();
var props:Object = new Object();"@graphnick";
var mySprite:Sprite = new Sprite();
// alternative way to add your object references
//spriteProperties[mySprite] = {name:"@graphnick", url:""}

So, now whenever you need to find the properties of mySprite, you can use the spriteProperties dictionary for the lookup the objects as such:

trace(spriteProperties[mySprite].name) // returns @graphnick
trace(spriteProperties[mySprite].url) // returns

Declaring a Dictionary in a Loop

I love using XML and creating buttons with the XML references. So when setting up my button list with the XML data, I will loop through the button nodes and set a dictionary key for each button using the button’s XML node to the value of the dictionary. All the buttons will use the same dictionary, but the key object (the object between the brackets) will be the buttons themselves.

import fl.controls.Button;
var myXML=new XML(){
var buttons_list:XMLList = myXML.button;
var propDict:Dictionary = new Dictionary(); // instantiate the Dictionary outside of the loop
var _idx:int=0;
// generate the buttons through a loop using the XML
for each(var button:XML in buttons_list){
	var cur_btn:Button=new Button();
	propDict[cur_btn] = button; // pass through the XML object to the dictionary
	cur_btn.addEventListener(MouseEvent.CLICK, checkDictionary);
function checkDictionary(event:MouseEvent){

Test it out:

Get Adobe Flash player

As stated before, a dictionary can reference any type of Object. This means that you can reference not only strings, XML, and Arrays, but Display Objects as well. For example, if you want to tie an image to a dictionary you can simply set the dictionary’s value to that Display Object with the associated key.


The Dictionary Class is a great tool that can be handy when you need to hold a reference to some data or other objects for future use. The great thing about the Dictionary class is the versatility in its use. With the way the dictionary can be implemented so dynamically, it really can be a powerful tool. So, go and try it. Perhaps you will also discover great uses in the dynamic Dictionary Class.

  • Lida_ami

    greate article.thanks

  • Henrique Oliveira

    Although many people think that the flash with Actionscript is ‘dying’, I do not think so.
    That was just an opinion of mine. This class has a lot of power. For example if you want to control the properties of a menu item. As described in the post, so if a DisplayObject, only the class MovieClip is a dynamic class, different class Sprite. But as the class Sprite uses less memory than the MovieClip class, it becomes more feasible to use the Dictionary class in conjunction with Sprite to create dynamic properties.
    Nice post!

    • Nicholas Thane Haroldsen

      Thank you for your comments Henrique. Flash is far from dead!