0

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 like:

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

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

2 Comments

Edit your question and put your output and explain in more detail what kind of error you are receiving.
Thanks very much. I had tried that before btw and the reason it failed for me is I was instantiating my page classes deep in the script in a switch, so it was tracing null. When I put page1:PageOne = new PageOne before the constructor it all worked. Thanks again!!

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.