Skip to main content
SabelV
Participant
July 8, 2011
Answered

How do I get a movie clip to loop a specific number of times in Flash?

  • July 8, 2011
  • 1 reply
  • 5705 views

Does anyone know how to get a movie clip to loop a specific number of times in Flash? I know how to stop a movie clip from looping by using the this.stop (); command by placing the command in a separate Action Script layer, in a keyframe, inside of the movie clip's timeline. This allows the movie clip to play through once and then stop. But I need for the movie clip to loop more than once, maybe 2 or 3 times, and then go back to the main timeline. Does anyone know the code for this?

Also, is it possible to place a pause (I'm guessing, maybe by using a timer of some type?) between the loops, so that the movie will pause a couple of seconds before it loops again and then stop? Please note I do not need the movie clip to stop when there's an event like a rollover or anything. I just need it to play a couple of times, pause between plays and then stop and go back to the main timeline. Please let me know if anyone can help.

Thanks,

Sarah

P.S. Is there a good, easy to use, reference book anyone can recommend for creating specific things in Flash using Action Script? Do you guys have a favorite for beginners like me?

This topic has been closed for replies.
Correct answer

There are various ways to do this... the most straightforward probably is to place a bit of code directly in the timeline of your clip. Assuming your frame 1 is a 'paused' frame with a stop() in it, you can add the following:

var loopCounter:int = 0;

stop();

Then in the last frame of the clip:

loopCounter++;

if(loopCounter == 3){

     dispatchEvent(new Event("clipDoneLooping"));

     gotoAndStop(1);

}else{

     gotoAndPlay(2);

}

So, in your main time line you have some condition that tells the clip to play with a gotoAndPlay(2); If you need to know the clip has looped three times and is finished you can listen for the clipDoneLooping to be dispatched.

myClip.addEventListener("clipDoneLooping", doSomething);

And the doSomething function will be called.

To place a pause between the loops you can use a timer object. Instead of the gotoAndPlay(2) in the conditional above, you would just start a timer.

1 reply

Correct answer
July 8, 2011

There are various ways to do this... the most straightforward probably is to place a bit of code directly in the timeline of your clip. Assuming your frame 1 is a 'paused' frame with a stop() in it, you can add the following:

var loopCounter:int = 0;

stop();

Then in the last frame of the clip:

loopCounter++;

if(loopCounter == 3){

     dispatchEvent(new Event("clipDoneLooping"));

     gotoAndStop(1);

}else{

     gotoAndPlay(2);

}

So, in your main time line you have some condition that tells the clip to play with a gotoAndPlay(2); If you need to know the clip has looped three times and is finished you can listen for the clipDoneLooping to be dispatched.

myClip.addEventListener("clipDoneLooping", doSomething);

And the doSomething function will be called.

To place a pause between the loops you can use a timer object. Instead of the gotoAndPlay(2) in the conditional above, you would just start a timer.

July 8, 2011

<blockquote><font size="1"><font color="800080"> use a timer object. Instead of the gotoAndPlay(2) in the conditional above, you would just start a timer.</font></font></blockquote>

Just a couple of extra lines (for the beginner...)

Add in frame 1

var myTimer:Timer = new Timer(1000, 1); // first value in the parenthesis defines your pause interval in milliseconds

myTimer.addEventListener(TimerEvent.TIMER, letsGoFurther_handler);

function letsGoFurther_handler(event:TimerEvent):void

{

     myTimer.reset();

     gotoAndPlay(2);

}


Add in the last frame instead of gotoAndPlay(2);

myTimer.start();

And

myTimer.removeEventListener(TimerEvent.TIMER, letsGoFurther_handler);

right after gotoAndStop(1);