user2072135
user2072135

Reputation: 59

Can currentFrame and totalFrame be pushed into a for loop in as3?

Okay, long story short I'm not near a compiler, but I need to know if I could write something like

for (currentFrame; currentFrame < totalFrames; currentFrame + 19;){
//do some animation
}

What I'm trying to do is have an array filled with objects, and every 19 frames, have the next objects in the array do some animation, so would the above be a valid way to do this? If not, could you show me how to do it?

Thank you in advance and sorry for the noobish question.

Upvotes: 0

Views: 254

Answers (1)

Vesper
Vesper

Reputation: 18747

You can use an enter frame listener to count frames. This also allows you to not care about the main MC's timeline (make it 1 frame or use a Sprite).

var yourArray:Array=[...]; // your array, pre-filled
var nowPlaying:int; // index in the array
var frame:int=0; // the frame counter
this.addEventListener(Event.ENTER_FRAME,animateChildren);
function animateChildren(e:Event):void {
    frame++;
    if (frame==19) {
        frame=0; // reset counter
        nowPlaying++; // next item to call play()
        if (nowPlaying==yourArray.length) nowPlaying=0; // loop the array
        // so the first item plays after last item
        var mc:MovieClip=yourArray[nowPlaying] as MovieClip;
        if (mc) mc.gotoAndPlay(1); // play the MC in question
    }
}

The code can be safely put into a class file instead of timeline, just separate variables into class vars, addEventListener() call into constructor, and function into class function.

Upvotes: 1

Related Questions