开发者

Help converting string based array to class variable name

开发者 https://www.devze.com 2023-03-09 05:24 出处:网络
I have an array, which are the literal names of class references.Eg. in my main class I have var page1:PageOne =new PageOne();

I have an array, which are the literal names of class references. Eg. in my main class I have

var page1:PageOne =   new PageOne();
var page2:PageTwo =   new PageTwo();
var page3:PageThree = new PageThree();

var sectionsArray = new Array ('page1', 'page2', 'page3')

What I'd like to write, but can't is:

var sectionsArray = new Array (page1, page2, page3)  

I am trying to tween something based on these values, but since the value is of type String, I cannot associate these values with the class references they represent. So I tried something l开发者_开发百科ike:

var tweenObj:Object = _sectionsArray[0] as Object
TweenLite.to(tweenObj, 1, {alpha:0});

But all this does is make it an Object of type String (and throw a tweenLite error because i tried to tween a string), which does not help me.

What is a better way to think about handling what I'm trying to do?

Thanks very much in advance!!


You can use [] initiation instead. Not only will it work with what you are trying to achieve it's also faster than using new.

var sections:Array = [page1, page2, page3];

Then when using the object you simply do:

TweenLite.to(sections[0], 1, {alpha:0});

Since all the objects in the array already the right type you don't need to typecast them before using them with Tweenlite.

Proof of concept:

var page1:Sprite = new Sprite();
var page2:Sprite = new Sprite();
var page3:Sprite = new Sprite();

var sections:Array = [page1, page2, page3];

trace(sections[0]);
trace(sections[1]);
trace(sections[2]);

Output:

[object Sprite]
[object Sprite]
[object Sprite]
0

精彩评论

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