Uğur Cemil Yılmaz
Uğur Cemil Yılmaz

Reputation: 53

How can I call function or object name in string variable? actionscript3

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..

Upvotes: 1

Views: 1685

Answers (2)

pho
pho

Reputation: 25489

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]

Upvotes: 3

Sam DeHaan
Sam DeHaan

Reputation: 10325

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.

Upvotes: 2

Related Questions