How to Loop a Movie Three Times (AS2)
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.
April 17th, 2006 at 11:11 am
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.
April 17th, 2006 at 11:53 am
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.
July 6th, 2006 at 3:17 pm
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
July 6th, 2006 at 3:50 pm
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:
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
loopCountwill be declared, thanks to the firstifstatement. It will immediately increment to 1. The secondifwill evaluate totrue, 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,loopCountincrements to 2 — repeat. Finally, when 200 is encountered a third time, the secondifevaluates tofalse, because 3 is not less than 3. Because of this, thatthis.gotoAndPlay(1);is not executed and the playhead continues on to frame 300 … eventually. On frame 300, put a simplethis.stop();to stop the playhead at that point.October 24th, 2006 at 8:37 am
[…] 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. […]
October 30th, 2006 at 3:49 pm
thanks for this..you could also entitle it..dealing with advertising constraints or help with flash ad loops.
cheers
todd
November 7th, 2006 at 10:46 am
found this tutorial useful. Thank you!
November 16th, 2006 at 10:37 pm
Hey David… it was really very helpful to me. Thanks for the tutorial.
November 17th, 2006 at 5:27 pm
double plus, todd, Katie, Nandkumar … thanks!
January 3rd, 2007 at 5:30 pm
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.
January 3rd, 2007 at 9:52 pm
Dave,
Hey, good to hear from you!
You make a good point. Declare the variable on frame 1, then omit the
ifstatement, 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.
January 17th, 2007 at 12:15 am
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
January 17th, 2007 at 11:52 am
Ze Canonero,
I don’t see how this is related to looping a movie three times, sorry!
January 21st, 2007 at 9:38 pm
This was a big help! Thanks!
April 3rd, 2007 at 10:15 am
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…
April 3rd, 2007 at 9:16 pm
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?
April 4th, 2007 at 7:06 am
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:
Notice the missing
varandthis.April 30th, 2007 at 12:53 pm
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!
April 30th, 2007 at 1:02 pm
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.
May 30th, 2007 at 5:48 pm
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?
May 30th, 2007 at 11:48 pm
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?
July 12th, 2007 at 9:21 am
The comments haven’t helped at all it’s the original that works fine for me
July 12th, 2007 at 12:11 pm
Ben,
I’m surprised none of the comments worked for you, but glad the original example did.
November 2nd, 2007 at 9:17 pm
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.
November 3rd, 2007 at 10:56 am
SeasFlame,
Love to hear that sort of thing; thanks!
November 5th, 2007 at 5:35 am
Simple, well written and very helpful. Another very happy designer here.
November 5th, 2007 at 6:54 pm
Derek,
Thanks a lot!
November 13th, 2007 at 10:15 am
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
November 13th, 2007 at 11:26 am
Rich,
Glad to hear it!
November 28th, 2007 at 7:24 pm
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.
November 29th, 2007 at 9:27 am
Vicki,
Glad to help.
November 30th, 2007 at 11:17 am
Thanks Dave, this is exactly what I needed!
November 30th, 2007 at 12:07 pm
tunghoy,
Glad it worked for you.
December 6th, 2007 at 11:47 am
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…)
December 14th, 2007 at 11:14 am
Torbot,
For something like that, you could set the
Button._visibleproperty of your button symbol totrue, inside thatifstatement.… which means you would have had to set that same button’s property to
falsein frame 1, and the symbol would have to span the whole length of the timeline so that the setting totruewould take. Make sense?December 18th, 2007 at 9:50 pm
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.)
December 19th, 2007 at 4:22 pm
sufgania,
For that, you’ll want “How to Pause a Timeline (AS2).”
January 7th, 2008 at 6:21 am
thanks, im a nubbie. finally found the action i needed.
January 10th, 2008 at 3:42 pm
gabe,
Glad to hear it.
February 4th, 2008 at 6:29 am
David
Bacon: Saved.
Many thanks.
February 4th, 2008 at 9:48 am
DaveC,
Sweet!
February 12th, 2008 at 4:55 am
You saved my night! Thank you!!!!
February 12th, 2008 at 9:14 am
Veronica,
Yay! Glad to help.
September 17th, 2008 at 3:18 pm
Just bookmarked this site. The world needs more simple solutions like this.
October 27th, 2008 at 1:23 am
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!
October 27th, 2008 at 9:26 am
To Jose …
Cool, thanks!
To Mike …
That’s high praise! Probably a bit too high, but thanks in any case.
Glad I could help.
October 29th, 2008 at 1:19 am
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.
October 29th, 2008 at 9:09 am
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:
… 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?
October 29th, 2008 at 1:12 pm
Thank you very much. One might say you are a godsend.