0
public class ColorLibrary {
    private var _allColorCodes:Object;

    public function ColorLibrary() {
        init();
    }

    private function init(){
        _allColorCodes = {
            'black'     :   '000000',
            'white'     :   'FFFFFF',
            'yellow'    :   '000000'
        }
    }

    public function exchangeColor(colors:String){
        var colorArray:Array = colors.split(',');
        for ( var i:int = 0; i < colorArray.length; i++ ) {
            _allColorCodes.getDefinitionByName(colorArray[i]);
        }

    }

}

any idea how to convert string to instance name? Thanks very much~! Strugglying here

1
  • What do you want to accomplish with your function (exchangeColor)? Commented Sep 19, 2011 at 7:30

2 Answers 2

2

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]
        );
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the quick reply, but my situation is i have a string of colors var color:String = 'black, white, yellow'; and i use split to convert array then use this array to looks up the object and return the hex number. i want to do this _allColorCodes.colorArray[i], but this won't work. any ideas?
Wow this is very fancy, first time see this. NICE i will give it a try. Thanks very much
1

I can´t see why you would want to do it like this, but here is a function that passes a list of colors (String) and returns hex codes (Array). If one color does not exist you will get lost in the Array. I Recommend using ColorList.RED or at least ColorManager.getColor("red").

var colorList : Array = getColorList("black,yellow");

public function getColorList(colors : String) : Array
{
    var result : Array = new Array();
    var colorArray : Array = colors.split(',');

    for(var i : int = 0; i < colorArray.length ; i++)
    {
        if(_allColorCodes[colorArray[i]])
            result.push(_allColorCodes[colorArray[i]]);
    }

    return result;
}

1 Comment

Wow Thanks very much, your solution is the one i am after, and it works perfectly. want to vote up your solution, but my reputation is not enought :( again thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.