Dictionaryを試してみる。

DictionaryObjectは、StringをキーにしてObjectを取り出せるわけだけど、DictionaryはObjectをキーにObjectを取り出せるのです。

▼Wonderfl

▼ActionScript AS3(FP9)
[sourcecode language=”as3″]
/*
Objectは、StringをキーにしてObjectを取り出せるわけだけど、
DictionaryはObjectをキーにObjectを取り出せるのです。
*/
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.utils.Dictionary;

/**
* …
* @author umhr
*/
public class Main extends Sprite
{

public function Main():void
{
var mySprite:Sprite = new Sprite();
var myBitmap:Bitmap = new Bitmap();
var myTextField:TextField = new TextField();
var myObject:Object = new Object();
var myFunction:Function = new Function();

var mySprite2:Sprite = new Sprite();
var myObject2:Object = new Object();

var dic:Dictionary = new Dictionary();
dic[mySprite] = "sp!";
dic[myBitmap] = "bm!";
dic[myTextField] = "tf!";
dic[myObject] = "ob!";
dic[myFunction] = "fn!";

dic[mySprite2] = [1,2,3];
dic[myObject2] = {hoge:"foo"};

var txt:String = "";
txt += "mySprite" + ":" + dic[mySprite] + "\n";
txt += "myBitmap" + ":" + dic[myBitmap] + "\n";
txt += "myTextField" + ":" + dic[myTextField] + "\n";
txt += "myObject" + ":" + dic[myObject] + "\n";
txt += "myFunction" + ":" + dic[myFunction] + "\n";

txt += "*************\n";

txt += "mySprite2" + ":" + dic[mySprite2] + ":" + (dic[mySprite2] is Array) + "\n";
txt += "myObject2" + ":" + dic[myObject2] + ":" + (dic[myObject2] is Object) + "\n";

txt += "*************\n";

for (var str:Object in dic) {
txt += str.toString() +":" + dic[str] + "\n";
}

var tf:TextField = new TextField();
tf.text = txt;
tf.wordWrap = true;
tf.width = tf.height = 465;
addChild(tf);
}
}
}
[/sourcecode]