Skip navigation
windowswarrior
Currently Being Moderated

Loop swf 3 times and stop

Sep 11, 2008 3:57 PM

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

 
Replies
  • Currently Being Moderated
    Sep 11, 2008 6:05 PM   in reply to windowswarrior
    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.h tml

    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."


     
    |
    Mark as:
  • Currently Being Moderated
    Sep 11, 2008 6:59 PM   in reply to Newsgroup_User
    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."


     
    |
    Mark as:
  • Currently Being Moderated
    Sep 11, 2008 7:29 PM   in reply to windowswarrior
    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."


     
    |
    Mark as:
  • Currently Being Moderated
    Sep 11, 2008 8:53 PM   in reply to windowswarrior
    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."


     
    |
    Mark as:
  • Currently Being Moderated
    Mar 11, 2010 11:33 AM   in reply to Newsgroup_User

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points