Jump to content
Search Community

AS3 Loop for Tweenlite [SOLVED]

leene test
Moderator Tag

Recommended Posts

Hi There,

 

I'm trying to clean up my code. Say I have 8 movieclips that I want to fade in at different times...

 

writing it this way seems to work but how would I write it to be in a loop instead of repeating it so many times?

import gs.TweenLite;
import gs.easing.*

TweenLite.to(mc1, Math.random()*50, {alpha:1});
TweenLite.to(mc2, Math.random()*50, {alpha:1});
TweenLite.to(mc3, Math.random()*50, {alpha:1});
TweenLite.to(mc4, Math.random()*50, {alpha:1});
TweenLite.to(mc5, Math.random()*50, {alpha:1});
TweenLite.to(mc6, Math.random()*50, {alpha:1});
TweenLite.to(mc7, Math.random()*50, {alpha:1});
TweenLite.to(mc8, Math.random()*50, {alpha:1});

------------------------------------------------------

 

Here was my attempt at the loop. But it has errors. :(

var movieClips:Number = 1; //tracks how many movie clips
addEventListener(Event.ENTER_FRAME,myEnterFrame);
function myEnterFrame(event:Event) {
 if (movieClips <= 8) {
	 var mc:String = "mc"+movieClips;
	 trace(mc);
	 TweenLite.to(mc, Math.random()*50, {alpha:1});
       movieClips += 1;
   }
}

Thanks!

Link to comment
Share on other sites

Do you want them to all start fading in at the same time, but some take longer than others to reach an alpha of 1 or do you want them to all take the same amount of time to fade in, but stagger their start times randomly? I assume you want option 2:

 

var clips:Array = [mc1, mc2, mc3, mc4, mc5, mc6, mc7, mc8];
for (var i:int = 0; i     TweenLite.to(clips[i], 2, {alpha:1, delay:Math.random() * 50});
}

 

There are a few optimizations we could make to this code, but it should give you the general idea. Basically we use a random delay (between 0 and 50 seconds) and make them fade in for 2 seconds, so everything should be finished by exactly 52 seconds.

Link to comment
Share on other sites

  • 6 months later...

This works perfectly, is there a way to make it so the tweens are random- but don't start until the last one finishes?

 

ie: mc5 starts/finishes > mc2 starts/finishes > mc8 starts/finishes

 

i guess it wouldn't be random start times, more like random selection of the animation order...

Link to comment
Share on other sites

Sure, you mean like this?:

var clips:Array = [mc1, mc2, mc3, mc4, mc5, mc6, mc7, mc8];
var timeline:TimelineLite = new TimelineLite(); //makes it simple to sequence
var temp:Array = clips.slice(); //make a copy because we'll be removing elements as we go
var i:int = temp.length;
var index:uint;
while (--i > -1) {
   index = int(Math.random() * i);
   timeline.append( new TweenLite(temp[index], 2, {alpha:1}) );
   temp.splice(index, 1);
}

 

(that's untested code, but hopefully it demonstrates the concept)

Link to comment
Share on other sites

Hey Jack,

 

That worked perfectly, totally awesome and this code will be very useful for many things. I'm trying to convert that to AS2, no errors- but nothing happens. Am I missing something?

 

import com.greensock.*;

var clips:Array = [mc1, mc2, mc3, mc4, mc5, mc6, mc7, mc8, mc9, mc10, mc11, mc12, mc13, mc14];

var timeline:TimelineLite = new TimelineLite();
var temp:Array = clips.slice();
var i:Number = temp.length;
var index:Number;

while (--i>-1) {
index = Number(Math.random()*i);
timeline.appendMultiple(TweenMax.allTo(temp[index], globalDuration, {_xscale:150, ease:Cubic.easeInOut}, 0.2));
temp.splice(index,1);

}

Link to comment
Share on other sites

  • 1 year later...

Thank you for help :-)

 

I think i´m close but there is still error :-(

I need all movieclips to fade out randomly to alpha:0,

Please take a look help sombody becouse I´ve got arrayNightMare :-))))) alrady..

 

var allMovies:Number = container_mc.numChildren;

var clips:Array = new Array ;

var eachMovie:MovieClip = new MovieClip ;

 

 

for (var i:Number = 0; i < allMovies; i++)

{

eachMovie = container_mc.getChildAt(i) as MovieClip;

trace(eachMovie);

clips.push(eachMovie);

doStuff();

}

 

 

function doStuff()

{

var timeline:TimelineLite = new TimelineLite();

var temp:Array = clips.slice();

var i:int = temp.length;

var index:uint;

while (--i > -1)

{

index = int(Math.random() * i);

timeline.append( new TweenLite(temp[index], 1, {alpha:0}) );

temp.splice(index, 1);

}

}

 

 

Carl PLEASE save me from this nightmare... :-)))

Link to comment
Share on other sites

the biggest problem that I see is that you are calling doStuff() inside your loop.

 

if you have 300 children movie clips.

 

you are creating 300 timelines.

 

you want 1 timeline, with 300 tweens inside it.

 

only call doStuff() after the loop runs.

 

you may still have errors, but I'm pretty sure you will have better luck once that issue is resolved.

Link to comment
Share on other sites

just do this

 

import com.greensock.*;
import com.greensock.easing.*;

var allMovies:Number = container_mc.numChildren;
var clips:Array = new Array  ;
var eachMovie:MovieClip;
var timeline:TimelineLite = new TimelineLite();//makes it simple to sequence

for (var i:Number = 0; i {
eachMovie = container_mc.getChildAt(i) as MovieClip;
clips.push(eachMovie);

}

doStuff();






function doStuff()
{

clips = shuffleArray(clips);
var i:int = clips.length;

while (--i > -1)
{

	timeline.append( new TweenLite(clips[i], .3, {alpha:0}));

}


}


//shuffle function lifted from Matt Maxwell: http://mattmaxwellas3.blogspot.com/2010/10/as3-shuffle-array.html

function shuffleArray(arr:Array):Array
{
var arr2:Array = [];
while (arr.length > 0)
{
	arr2.push(arr.splice(Math.round(Math.random() * (arr.length - 1)), 1)[0]);
}
return arr2;
}

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