How to Loop a Flash Movie a Set Number of Times

by carl schooff on May 18, 2010 · View Comments

in Uncategorized

This video tutorial will show you how to make a Flash movie loop a certain number of times. With very basic ActionScript you can literally count how many times your animation has played and stop when the counter reaches a certain point.

The code from this example can very easily be copied and pasted into any document. What you choose to do when your animation is done playing is up to YOU!

Here is some code to steal for your own use:

ActionScript 2

Frame 1:

var count:Number = 0;
var maxTimesToPlay:Number = 3;

Last Frame of Animation:

count++;
count_txt.text = count;

if(count == maxTimesToPlay){
     gotoAndPlay("allDone");
}else{
     gotoAndPlay(2);
}

ActionScript 3

Frame 1:

var count:Number = 0;
var maxTimesToPlay:Number = 3;

Last Frame of Animation:

count++;
count_txt.text = String(count);

if(count == maxTimesToPlay){
     gotoAndPlay("allDone");
}else{
     gotoAndPlay(2);
}

* The only difference with the AS3 version is highlighted above. The count variable needs to be converted to a String before being displayed in the textfield.

Here are the Flash CS3 source files with AS2 and AS3 Versions: playMovieSetNumberOfTimes.zip

Post to Twitter Post to Facebook

  • http://www.bistado.com/news modus_operandi

    if(rpt_counter == null) var rpt_counter:Number = 0;
    rpt_counter++;
    if(rpt_counter == maxLoop) stop();

  • http://www.bistado.com/ modus_operandi

     just put this script on the last frame :)

  • Surfandturfsd

    I had to add on the first frame using Carl’s code:

    var count_txt:TextField = new TextField();

    otherwise it consider count_txt as undefined.

blog comments powered by Disqus

Previous post:

Next post: