You've already got an object there, so you can already go:
_allColorCodes.black .. etc
Considering that _allColorCodes is private, you can do a really nice little getter like so:
public function get colorCode():Object
{
return _allColorCodes;
}
And then just have:
trace(colorCode.black);
trace(colorCode.yellow);
All that said, what I would do is store all this stuff against constants in a class like ColorCodes, like this:
package
{
public class ColorCodes
{
public static const BLACK:uint = 0x000000;
public static const RED:uint = 0xFF0000;
public static const BLUE:uint = 0x0000FF;
public static const GREEN:uint = 0x00FF00;
public static const WHITE:uint = 0xFFFFFF;
}
}
And then access them via:
trace(
ColorCodes.RED
);
Something slightly more advanced that you can do is make use of Proxy and flash_proxy to override the getProperty() method. This means you'll be able to go ColorLibrary.color straight off the bat:
package
{
import flash.utils.Proxy;
import flash.utils.flash_proxy;
public class ColorLibrary
{
private var _allColorCodes:Object;
public function ColorLibrary()
{
_allColorCodes = {
'black' : '000000',
'white' : 'FFFFFF',
'yellow' : '000000'
}
}
override flash_proxy function getProperty(name:*)*
{
return _allColorCodes[name];
}
}
}
Response to comment:
Okay, you don't need to use getDefinitionByName here, you can simply use brackets to access a value of an object by parsing a string.
Example:
var object:Object = {
something: 2,
anotherThing: "something awesome"
};
trace(object["something"]); // same as object.something
trace(object["anotherThing"]); // same as object.anotherThing
Try something like this for your exchangeColor() function:
public function exchangeColor(colors:String):void
{
var colorArray:Array = colors.split(',');
for each(var i:String in colorArray)
{
trace(
_allColorCodes[i]
);
}
}