1

I need to call an object or function name with string variable.

For example;

var item_1:Object;
var str:String = "item_1";
TweenLite.to(item_1, 2, {alpha:0});

it's working but,
if I do something like below its not working

var item_1:Object;
var str:String = "item_1";
TweenLite.to(str, 2, {alpha:0});

how can do that? thanks from now..

0

2 Answers 2

3

Try something like

this[str];
//or
root[str]

All objects in AS3 can be accessed as object[key]=value

So, if you know where item_1 is declared, you can call it as itemParent["item_1"] or in your example, itemParent[str]

Sign up to request clarification or add additional context in comments.

Comments

2

I'm having some trouble reading your question, but it seems like you're looking for the following:

var item_1:Object
var str:String = "item_1"
TweenLite.to(this[str], 2, {alpha:0});

To dynamically access objects by their id from a string, you need to use the this["itemid"] notation.

3 Comments

@sch Ah. I've never encountered the need to use this notation, so I'm not familiar with its limitations. Thanks.
ah dammit! undone by a slow internet connection!! :P @sch you're right, but I guess we should get the benefit of the doubt, no?
Yeah I believe that the "dynamic" keyword in flash should be completely removed. Dynamic resolution can ONLY promote poor coding practice and as such will always lead to slower performance. That's why you never see any of the mature languages employ such an atrocious feature.

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.