9 Replies Latest reply: Mar 11, 2010 11:33 AM by mr_alioto RSS

    Loop swf 3 times and stop

    windowswarrior Community Member
      I was sure that something would be posted here on writing action script to make a flash banner loop only 3 times since this is a fairly common standard for web banner advertising. I've been unable to find this help or to make it work on my own.

      My fla file has 85 frames. I tried adding this action script to the 85th frame, but it doesn't seem like the var increases just upon reaching frame 85. It's increasing multiple times in it's first run from frame 0 to 85. I thought it wouldn't increase until it went back to 1 and reached 85 again.

      Cannot seem to get ATTACH CODE to go between paragraphs. This is my last paragraph to read after code:

      There must be an easier was to achieve this, but having spent 2 hours on it and searching online, I cannot figure it out. Do you know? Oh, I should probably mention that I started out with this Action Script at Frame 1 and that didn't seem to work either.

      Thanks in advance,
      Kristi

        • 1. Re: Loop swf 3 times and stop
          Newsgroup_User Community Member
          windowswarrior,

          > I was sure that something would be posted here on
          > writing action script to make a flash banner loop only
          > 3 times since this is a fairly common standard for web
          > banner advertising.

          It's definitely a common question! Not just looping, but particularly
          the number three. Since you're posting in the AS3 forum, here's an
          ActionScript 3.0 solution:

          http://www.adobe.com/designcenter/flash/articles/flacs3it_astimeline.html

          I'm the friendly looking guy in the black-and-white photo, not the
          toothy guy in the color photo. (Just having a chuckle at Tom, lol ... he
          does this sort of thing to me all the time. He deserves it.)

          Joking aside, flip through that tutorial and see if that makes sense to
          you. If not, write back and I'll be happy to answer particular questions.
          Here's a version for ActionScript 2.0, as well:

          http://www.quip.net/blog/2006/flash/how-to-loop-three-times


          David Stiller
          Co-author, The ActionScript 3.0 Quick Reference Guide
          http://tinyurl.com/2s28a5
          "Luck is the residue of good design."


          • 2. Re: Loop swf 3 times and stop
            windowswarrior Community Member
            Working in Flash CS2 so I'm pretty sure it's AS3. However, your suggestion doesn't work. It just keeps looping.

            I've pasted this in a keyframe on frame 1 of ACTIONS

            var loop:Number = 0;

            On the last frame I've added another keyframe and edited the script slightly so that the loop doesn't go all the way to the start.

            loop = loop + 1;
            if (loop < 3) {
            this.gotoAndPlay(85);
            } else {this.stop();
            }

            The swf file is looping back to frame 2 and continues indefinitely.

            Also found this post in a FLASH site, but perhaps it's script 2, so it doesn't stop after 3 times either.

            Frame 1

            var counter = 1
            var maxLoop = 3

            Last Frame

            if(counter > maxLoop){
            stop();
            }else{
            counter = counter + 1;
            gotoAndPlay(85);
            }

            This script also makes the playhead return to frame 2 and loop indefinitely.
            • 3. Re: Loop swf 3 times and stop
              Newsgroup_User Community Member
              windowswarrior,

              > Working in Flash CS2 so I'm pretty sure it's AS3. However,
              > your suggestion doesn't work. It just keeps looping.

              Flash CS3 is the first in the Flash family to include a "CS" in the
              name, so you're either working with Flash CS3 (the first to support
              ActionScript 3.0) or ... maybe Flash 8? If the letter, then you'll have to
              use ActionScript 2.0 or lower. But now that I'm looking at this code again,
              the ActionScript 2.0 and 3.0 versions are so similar, it really doesn't
              matter in this case.

              Let's assume ActionScript 3.0, and we'll take it step by step.

              Bear with me and start a new FLA file, because if we start from scratch,
              it's a lot more likely we'll catch what's going wrong where.

              1) Select File > New and choose Flash File (ActionScript 3.0).

              2) Select frame 1 of the main timeline, open your Actions panel, and type
              this:

              var loop:int = 0;

              So far, we're doing nothing more than creating a variable (happens to be
              an integer) in frame 1 and setting it to 0. (Previous code showed :Number
              instead of :int, and honestly, either one will do. Because this number is
              only going to be an integer, :int is technically the better choice.)

              3) Add a keyframe to frame 10. Enter the following ActionScript in frame
              10:

              loop = loop + 1;
              trace(loop);
              if (loop < 3) {
              this.gotoAndPlay(2);
              } else {
              this.stop();
              }

              At this point, the variable, loop, is incremented by 1. The first time
              this happens, its value becomes 1 (because 0 + 1 is, of course, 1). Next, a
              trace() function traces the value of loop to the Output panel. You'll see
              "1", without quotes, appear in the Output panel when the playhead enters
              this frame.

              Next, an if() statement compares the value of loop (which is currently
              1) to the number 3. If it's less than 3 -- at this point, it is -- the
              playhead is sent back to frame 2. Otherwise, it stops.

              4) Test your movie. In short order, you should see the numbers 1, 2, and 3
              appear in the Output panel. After that, nothing else -- because the
              playhead has stopped at frame 10 and no longer loops.

              Let me know if that happens for you.


              David Stiller
              Contributor, How to Cheat in Adobe Flash CS3
              http://tinyurl.com/2cp6na
              "Luck is the residue of good design."


              • 4. Re: Loop swf 3 times and stop
                windowswarrior Community Member
                Decided to bail on the animation for now so I could get the ad to the site tomorrow. I deleted frame 1 across the board (thereby I THOUGHT removing var counter = 1 etc.).

                Tested the movie to see what other changes I might like to make AND IT WORKED. Switched back to frame 85 and only rotated 3 times.

                Not sure why, but I guess I misunderstood the "start annimation in frame 2". I didn't have any content in any of the layers in frame 1 except in the ACTIONS layer. Perhaps this is not right.

                When I shift selected to deleted the column of frames in all layers under frame 1, the actions from frame one move to the content of frame 2 and all former frame 2 content became frame 1. Then the script you provided worked.
                • 5. Re: Loop swf 3 times and stop
                  windowswarrior Community Member
                  I think the problem happened when I tried adding in a column of frames (new frame 1) to an already built FLA file. I did this because you (and another help I saw) said to start the animation on frame 2. I thought this basically meant to start the content on 2.

                  Perhaps this is what caused the problems.

                  Could you please comment on the correct way to add the script to an existing file? Obviously it's not to add a column of frames across the layers and move the existing keyframes to frame 2, and then add the script to frame 1 ACTIONS.
                  • 6. Re: Loop swf 3 times and stop
                    Newsgroup_User Community Member
                    windowswarrior,

                    > I think the problem happened when I tried adding in a column
                    > of frames (new frame 1) to an already built FLA file.

                    Shouldn't really matter -- as long as that variable declaration is
                    ultimately in the first frame of the FLA file.

                    > I did this because you (and another help I saw) said to start the
                    > animation on frame 2. I thought this basically meant to start the
                    > content on 2.

                    Your visual content can start in frame 1, 2, or wherever you like. The
                    key to the approach suggested by me (and this other suggestion you saw) is
                    to only allow the variable declaration to happen once. If the playhead
                    travels to frame 1 more than once, the loop variable will be reset back to
                    0, which means it will never properly increment.

                    > Could you please comment on the correct way to add the script
                    > to an existing file?

                    Personally, I always create a layer at the top, just for ActionScript.
                    I name it "scripts," and that way I know where all my code is.

                    > Obviously it's not to add a column of frames across the layers and
                    ? move the existing keyframes to frame 2, and then add the script to
                    > frame 1 ACTIONS.

                    Honestly, that should have worked just fine. But maybe you had script
                    elsewhere that conflicted with the suggested code.


                    David Stiller
                    Adobe Community Expert
                    Dev blog, http://www.quip.net/blog/
                    "Luck is the residue of good design."


                    • 7. Re: Loop swf 3 times and stop
                      windowswarrior Community Member
                      Yes, I actually had 2 layers called ACTIONS, and didn't notice it. When I deleted the 1 column of frames, I think it disabled the other script which was on the duplicate layer.

                      Thanks for the help. It's nearly midnight and I'm fried. You were invaluable to me :)
                      • 8. Re: Loop swf 3 times and stop
                        Newsgroup_User Community Member
                        windowswarrior,

                        > Thanks for the help. It's nearly midnight and I'm fried.

                        Heh ... me too. I really need to start going to bed earlier. I'll be
                        able to soon. Nearly finished a walloping project that has taken much
                        longer than it should have.

                        > You were invaluable to me :)

                        Glad to help!


                        David Stiller
                        Adobe Community Expert
                        Dev blog, http://www.quip.net/blog/
                        "Luck is the residue of good design."


                        • 9. Re: Loop swf 3 times and stop
                          mr_alioto

                          Thanks David.

                          I have for some time been looking for a remedy for the "loop 3 times then stop". I'm a novice at Flash, you are the first person who was clear on your description and the script worked. Kudos, I probably have read 30-40 ways from different people on all kinds of sites. I think maybe you should write a book on as3 script.

                          Thanks again,

                          mr_alioto