How to Loop a Movie Three Times (AS2)

Flash ActionScript 2.0

Note:  An ActionScript 3.0 version of this article is located in a more recent entry of this blog.

Truth be told, this article should actually be titled “How to Loop a Movie As Many Times as You Like.”  It just happens that when people ask about this topic, the most popular number seems to be three. 

An answer, short and sweet

Add a keyframe to the last frame of your movie.  Any layer will do, but if you like to be organized, create a dedicated scripts layer.  Click into this concluding keyframe and paste the following ActionScript.

if (!loopCount) {
  var loopCount:Number = 0;
}
loopCount++;
if (loopCount >= 3) {
  this.stop();
}

How it works

An if statement checks to see if a variable named loopCount exists.  If it doesn’t, this variable is created and initialized to a value of zero.  During the movie’s first run, loopCount won’t exist, of course, and will be created.  During subsequent runs, the variable creation will be ignored.

The ++ operator increments loopCount by one.

Another if statement checks to see if loopCount’s value is equal to or greater than three.  If it is, the MovieClip.stop() method is invoked on the main timeline, which halts the movie.  On the movie’s first and second run, loopCount will be less than three, so the movie will automatically loop.

Variations

If you want the movie to five times, change that three to a five.  For fifteen loops, change it to a 15.  You get the idea. :)   If you only want the movie to play once, scrap all of the above and simply put this.stop(); instead.

If you want the movie to loop, but not from the beginning — that is, start on frame 1, play through frame 300, then loop from 100 through 300 — alter the code like this:

if (!loopCount) {
  var loopCount:Number = 0;
}
loopCount++;
if (loopCount >= 3) {
  this.gotoAndPlay(100);
}

… replacing 100 with whatever frame number you please.

49 Responses to “How to Loop a Movie Three Times (AS2)”

  1. NSurveyor Says:

    A one liner :)

    !loop ? loop=1 : ++loop>=3 ? this.gotoAndPlay(100) : null;

    If loop is undefined/null/false/0, loop equals one, otherwise, increment loop. If it has reached 3, goto and play frame 100, otherwise do nothing.

  2. David Stiller Says:

    NSurveyor,

    There you go. ;)

    I chose a “several liner” approach for sake of demonstrating the principles involved, but I always appreciate your succinct code in the forums.

  3. Eric Says:

    Hey, Your tutorial is great. I knew script is pretty straight forward. Thanks.

    But, what if you want the movie to run 3 times, then stop on frame 300?

    thanks,
    e

  4. David Stiller Says:

    Eric,

    The article suggests putting ActionScript in the last keyframe of your movie and relying on the movie to automatically repeat. In your case, you could put the above “last keyframe” code in a frame before 300 — just forget about it being on the last keyframe anymore, because it no longer is. :) Then look how the code changes:

    if (!loopCount) {
      var loopCount:Number = 0;
    }
    loopCount++;
    if (loopCount < 3) {
      this.gotoAndPlay(1);
    }

    For sake of discussion, let’s say the above is on frame 200. The first time this frame is encountered by the playhead, the variable loopCount will be declared, thanks to the first if statement. It will immediately increment to 1. The second if will evaluate to true, because 1 is less than 3; therefore, the movie is sent to frame 1 and told to play again. When it again hits frame 200 a second time, loopCount increments to 2 — repeat. Finally, when 200 is encountered a third time, the second if evaluates to false, because 3 is not less than 3. Because of this, that this.gotoAndPlay(1); is not executed and the playhead continues on to frame 300 … eventually. On frame 300, put a simple this.stop(); to stop the playhead at that point.

  5. double plus good blog » Blog Archive » Online Advertising 101 Says:

    […] Max loops It seems silly to me but some sites have a limit on how many times your animation can loop. I imagine they are trying to keep the aesthetics of their page as clean as possible without a bunch of blinking and winking going on. I found an especially useful bit of ActionScript from David Stiller’s blog on how to achieve this. […]

  6. todd Says:

    thanks for this..you could also entitle it..dealing with advertising constraints or help with flash ad loops.
    cheers
    todd

  7. Katie Says:

    found this tutorial useful. Thank you! :)

  8. Nandkumar Says:

    Hey David… it was really very helpful to me. Thanks for the tutorial.

  9. David Stiller Says:

    double plus, todd, Katie, Nandkumar … thanks!

  10. Dave Says:

    This works nicely, but it’s not optimal. I think it better to initialize the variable first (frame 1) and then you can eliminate the first if statement… If you’re really picky about speed, or writing optimal code, I think it matters.

  11. David Stiller Says:

    Dave,

    Hey, good to hear from you! :)

    You make a good point. Declare the variable on frame 1, then omit the if statement, but loop to frame 2 each time, in order to avoid re-declaring (effectively resetting) the variable. In this case, I went for a single-frame approach because I thought it would be easiest for a non-coder to see, all at once. Plus, it introduces the idea on what it means to check for the existence of an object before operating on it.

    But to be sure, checking for the existence of that variable each time isn’t the leanest way to go. Thanks for bringing it up.

  12. Ze Canonero Says:

    Hey i’m developing a game in flash, but i ran into a problem that i cannot solve

    i have six buttons on the screen only one is right
    i need to code it so it performs the action of the right button if the wrong buttons are clicked (ramdonly) 3 times

    Thank’s in advance

  13. David Stiller Says:

    Ze Canonero,

    I don’t see how this is related to looping a movie three times, sorry!

  14. Jason Says:

    This was a big help! Thanks!

  15. John Ferguson Says:

    Thanks for this, Dave.

    I’m just doing a set of ads to go on MSN & they have this 3 loop constraint.

    This saved me finding out all sorts of things I didn’t yet know about actionscript, like whether variables are carried through more than one loop of the movie or re-initialised…

  16. John Ferguson Says:

    Spoke too soon! They also have the constraint that flash must be compatible with flash 4!!

    Fortunately I’ve still got MX installed so could access the actionscript 1.0 reference, but it turns out you can’t even declare variables until flash 5 so I’m a bit stumped.

    Any suggestions?

  17. David Stiller Says:

    John,

    Wow. MSN requires Flash 4?! That must be because MSN TV (formerly WebTV) still only supports that version of the Flash Player. Interesting. Flash 4 ActionScript can be a bizarre creature if you’re used to Flash 5 or later. Fortunately, in this case it’s not so hard:

    if (!loopCount) {
      loopCount = 0;
    }
    loopCount++;
    if (loopCount >= 3) {
      stop();
    }

    Notice the missing var and this.

  18. Brendan Says:

    Hi David
    Thanks for this tutorial. By using symbols within symbols, I have been able to limit my movie to 1 frame on the timeline. I made a new layer and added this actionscript to it and it has not worked to limit the number of loops. Is it that I need atleast 2 frames in my movie? Thank you!

  19. David Stiller Says:

    Brendan,

    In the original article, the ActionScript refers to the main timeline. Since you’ve put all your assets into a movie clip symbol, that has become your new “main timeline,” in a sense. The quickest way to use the above sample code is to move it inside the last frame of the movie clip that holds all your other content.

  20. helen silcock Says:

    this sounds as if it may be what I need but my banner ad stops at the end of the last movie and I can’t work out how to make it loop. Can you help?

  21. David Stiller Says:

    helen,

    If you want it to loop forever, put the following code into the very last keyframe of the main timeline:

    gotoAndPlay(1);

    If you want it to loop a certain number of times, the above code should do it, but I wrote it in ActionScript 2.0. Is that what your publish settings are configured for?

  22. Ben frustrated Brown Says:

    The comments haven’t helped at all it’s the original that works fine for me

  23. David Stiller Says:

    Ben,

    I’m surprised none of the comments worked for you, but glad the original example did. :)

  24. SeasFlame Says:

    This is the best tutorial. You are amazing. So clear. I am a designer on a deadline and I was able to pop this into my flash file and look like a real flash pro. Thank you 1000 times. Thank You.

  25. David Stiller Says:

    SeasFlame,

    Love to hear that sort of thing; thanks!

  26. Derek Says:

    Simple, well written and very helpful. Another very happy designer here.

  27. David Stiller Says:

    Derek,

    Thanks a lot!

  28. Rich Says:

    I have been seaching the net for two days and have tried more ways than I can remember to make this happen. This is the only piece of code i have found that works. Thankyou

  29. David Stiller Says:

    Rich,

    Glad to hear it!

  30. Vicki Says:

    Here’s a blog that originated in April 2006 - and people are still finding it useful over a year and half later! I also tried several other scripts today that were similar, but just didn’t work. This one worked like a charm. Thanks a bunch!! Really appreciate it.

  31. David Stiller Says:

    Vicki,

    Glad to help. :)

  32. tunghoy Says:

    Thanks Dave, this is exactly what I needed!

  33. David Stiller Says:

    tunghoy,

    Glad it worked for you. :)

  34. Torbot Says:

    great tutorial - really saved my arse! but what if the agency requires the movie to loop 3 times, then display a “replay” button? (I’m using AS2, BTW…)

  35. David Stiller Says:

    Torbot,

    For something like that, you could set the Button._visible property of your button symbol to true, inside that if statement.

    if (loopCount >= 3) {
      this.stop();
      myButton._visible = true;
    }

    … which means you would have had to set that same button’s property to false in frame 1, and the symbol would have to span the whole length of the timeline so that the setting to true would take. Make sense?

  36. sufgania Says:

    I would like to pause the movie for a few seconds at certain frames. How would you accomplish this?
    (I’m publishing to Flash 6 format.)

  37. David Stiller Says:

    sufgania,

    For that, you’ll want “How to Pause a Timeline (AS2).”

  38. gabe Says:

    thanks, im a nubbie. finally found the action i needed.

  39. David Stiller Says:

    gabe,

    Glad to hear it. :)

  40. DaveC Says:

    David

    Bacon: Saved.

    Many thanks.

  41. David Stiller Says:

    DaveC,

    Sweet!

  42. Veronica Says:

    You saved my night! Thank you!!!!

  43. David Stiller Says:

    Veronica,

    Yay! Glad to help. :)

  44. Jose Says:

    Just bookmarked this site. The world needs more simple solutions like this.

  45. Mike C. Says:

    David, you are my hero!!!

    I can’t tell you how much you’ve helped me. Thanks again!!

    There’s a special place in heaven for you!

  46. David Stiller Says:

    To Jose …

    Cool, thanks!

    To Mike …

    That’s high praise! Probably a bit too high, but thanks in any case. :) Glad I could help.

  47. John Says:

    Hi David, looks like your the man to talk to..

    I was able to make my banner loop a few seconds into the animation, but there is some type of delay where it blackscreens between takes. You can check it out on the enterance to my site.

    Thanks for your time.

  48. David Stiller Says:

    John,

    It looks like you might want to send your playhead back to some frame other than frame 1 — to whatever makes sense after the very beginning fade-from-black.

    Something like this might work:

    if (!loopCount) {
      var loopCount:Number = 0;
    }
    loopCount++;
    if (loopCount <= 3) {
      this.gotoAndPlay(80);
    } else {
      this.stop();
    }

    … where 80 is hypothetically a frame that fits in terms of looping but appears after the fade in. Obviously, you’ll need to find whatever number applies in your case, and you may need to tweak the animation to loop cleanly.

    Does that make sense?

  49. John Says:

    Thank you very much. One might say you are a godsend.

Leave a Reply