views:

50

answers:

2

I'm wondering, and hoping, if I can access and use a specific instance of an object if I only have the name of the object instance in a string? The code below perhaps explains it a littler better.

public function myFunction(){
    var myArbItem:mcArbItem = new mcArbItem();
    //mcSomeItem has another movieclip on it called 'itemLogo'

    //elsewhere there is an object called ArbItem
    ArbItem.addEventListener(MouseEvent.CLICK, showItem)
}

private function showItem(e:MouseEvent){
    var objectName:String = 'my' + e.target.name;
    //now I have the name of the object, that is myArbItem, can I with this
    //information e.g. set "myArbItem.itemLogo.visible = false;" 
    //or "addChild(myArbItem);"?
}
+3  A: 

Use the getChildByName function.

mcandre
Looks like eval isn't used anymore in AS3, your answer led me to the right function though getChildByName(). Cheers and thanks.
Bjorninn
You're welcome :)
mcandre
A: 

you can also do it the following way:

var objectName = ["my"]+e.target.name;

should force type it to a movie clip OR:

var objectName:MovieClip = ["my"]+e.target.name as MovieClip;

I have used these methods before and they have worked very well. I use it a lot in loops where I dynamically create objects and need to ref them later.

-Dig