开发者

Is it possible to turn a string into a reference to an object?

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.nam开发者_开发知识库e;
    //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);"?
}


Use the getChildByName function.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜