Jump to content
Search Community

Having trouble tweening every instance in a class

LanLing test
Moderator Tag

Recommended Posts

I have a class "interludeIntro", inside this class are tons of movieclips (each with own instance name interludeBegin1, interludeBegin2, etc). First I want to make these invisible, then use tweenlite to fade them in one by one. This is my code:

 

public function interludeBeginning():void {

  interludeIntro.alpha = 0; //set the whole class invisible

  TweenLite.to(interludeIntro.interludeBegin1, 0.5, {alpha:1,delay:1});
  TweenLite.to(interludeIntro.interludeBegin2, 0.5, {alpha:1,delay:2});
  TweenLite.to(interludeIntro.interludeBegin3, 0.5, {alpha:1,delay:3});
  TweenLite.to(interludeIntro.interludeBegin4, 0.5, {alpha:1,delay:4});
...
...//for all instances, fade in one by one
}

 

However this simply makes everything permanently invisible, despite the tween attempts.

 

This code below works, but in a very awkward/redundant/unnecessary way:

 

public function interludeBeginning():void {

 var interludeArray:Array = [interludeIntro.interludeBegin1, interludeIntro.interludeBegin2, interludeIntro.interludeBegin3, interludeIntro.interludeBegin4, ........ interludeIntro.interludeBegin15];
for each (var i:MovieClip in interludeArray) {
  i.alpha = 0; //set everything invisible
}
...
...
  TweenLite.to(interludeIntro.interludeBegin1, 0.5, {alpha:1,delay:1});
  TweenLite.to(interludeIntro.interludeBegin2, 0.5, {alpha:1,delay:2});
  TweenLite.to(interludeIntro.interludeBegin3, 0.5, {alpha:1,delay:3});
  TweenLite.to(interludeIntro.interludeBegin4, 0.5, {alpha:1,delay:4});
...
...//for all instances, fade in one by one
}

 

For some reason I gotta fade out each instance individually rather as a whole class. This is not practical because what if my class has 100 instances? Listing all of them out in an array is going to be a huge mess.

 

Thanks.

Link to comment
Share on other sites

Hi,

 

The results you are experiencing are fully in line with the fundamental workings of Flash.

 

imagine you have a box and put 5 hamsters in it. If you push the box across the room all the hamsters are going to move across the room with the box. make sense?

 

The same concept applies to Flash. If you set the alpha of a container to 0, all the children will appear invisible.

 

Also, since you wisely assigned all your child instances names chronologically, you shouldn't have to list them all in an array by hand.

 

you can just do something like:

 

var instanceCount:Number = 100;

for(var i:int = 1; i <=100; i++){
  interludeIntro["interludeBegin" + i].alpha = 0;
}

 

 

---

 

also, if the intrerludeBegin items are the only children of interludeIntro, you can just loop through all the children of interludeBegin and set their alpha to 0 without even knowing what their instance names are.

 

http://www.snorkl.tv/2011/09/actionscript-3-shuffle-array-and-loop-through-children/

 

---

 

oh, and you can also just do a TweenMax.fromTo() tween and completely avoid manually setting the alpha to 0.

 

TweenMax.fromTo(interludeIntro.interludeBegin1, 0.5, {alpha:0}, {alpha:1,delay:1});

 

http://www.greensock.com/as/docs/tween/com/greensock/TweenMax.html#fromTo()

 

note using this method you still have to write out all the tweens and assign their delays.

 

----

 

If you build an array of all your child instances, you can simply Tween them all with a single line of code:

 

TweenMax.allFromTo( arrayOfInstances, .5, {alpha:0}, {alpha:1}, 1)

 

http://www.greensock.com/as/docs/tween/com/greensock/TweenMax.html#allFromTo()

Link to comment
Share on other sites

Thank you carl.

I used

for(var i:int = 1; i <=100; i++){
  interludeIntro["interludeBegin" + i].alpha = 0;
}

Then individually applied TweenLite.to for each instance since I didn't want everything to appear at once, I wanted to add a 1sec delay after each instance appear. I looked over the link your provided on shuffling arrays, but it's kind of advanced and I'm not following the logic of the code. Can that tutorial simply go over each instance of a class (the interludeBegin instances are the only instances in the interludeIntro class) and fade each instance 1sec after another? Doing it manually I had delay:1, delay:2, delay:3,... etc

Link to comment
Share on other sites

if you use the technique in my tutorial, the downside is that you can't easily dictate the order in which things animate unless you are absolutely certain that each instance is at the proper depth.

 

so ignore that for now.

 

if you are going to use that loop (which is fine). try this:

 

var myInstances:Array = [];
for(var i:int = 1; i <=100; i++){
//add children to the array  
  myInstances.push( interludeIntro["interludeBegin" + i] )
}
//stagger the fade-in of each instance with a 1 second delay
TweenMax.allFromTo( myInstances, .5, {alpha:0}, {alpha:1}, 1);

 

this way you don't have to write out each tween.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...