A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Simple Flash web banner needs to loop 3x, how?

  1. #1
    Member
    Join Date
    Apr 2008
    Posts
    44

    Simple Flash web banner needs to loop 3x, how?

    Hi, Im not sure why this isnt working...

    I designed a very simple flash web banner and the requirement is to have it loop 3 times only. I entered this code at the last frame:

    sample:
    var i = 0;
    if (i < 4)
    {

    gotoAndPlay(1);
    i++;
    }
    else
    {
    stop();
    }

    trace(i);

    obviously it isnt working... It keeps tracing 1 hence it just loops forever.. I know this is simple but I cant seem to get it pls help, thanks

  2. #2
    Member
    Join Date
    Oct 2009
    Location
    Ontario
    Posts
    98
    That's because that variable is re-initialized to 0 every time you enter the frame.

    What you need to do is initialize the variable in a frame that is not re-visited, such as the first frame, and then have your last frame point to the second (gotoAndPlay(2)), so as to avoid re-initializing your counter to 0.

    Also, because you start with your counter at 0, this movie will actually loop 4 times. Either change the starting value to 1 or change the condition in the if-statement to be true only when i is less than 3, instead of 4. =)
    Last edited by Nidht; 10-13-2009 at 09:53 AM.

  3. #3
    Member
    Join Date
    Feb 2006
    Posts
    96
    Try this:

    PHP Code:
    var i:Number;

    for(
    i=14i++) { 
         if(
    i<4) {
              
    gotoAndPlay(1);
         }
         else { 
              
    stop();
         }
    }

    trace(i); 
    I don't know if this is what you're looking for, but from what I read, it accomplishes what you're trying to do.

    oops. thanks flo for pointing that out. I just cut and pasted and hadn't had my 2nd cup of coffee yet I think I might just stick to asking questions instead of trying to answer them.
    Last edited by compjock; 10-13-2009 at 09:26 AM. Reason: fix my stupid mistake.

  4. #4
    AS2 intolerant person
    Join Date
    Jan 2009
    Location
    Swansea
    Posts
    352
    i dont think you can use an 'if' statement like that, nidhts solution is proberly the best

  5. #5
    Member
    Join Date
    Oct 2009
    Location
    Ontario
    Posts
    98
    compjock, because of how you have the for-loop setup, it will actually loop forever (as is the same in the original post). Every time the movie hits that frame, it sets i back to 1. Also, when the if-statement is satisfied, it will go to the first frame and the loop will be interrupted.

    This is modified code for your final frame:

    Code:
    if (i < 4) {
    	gotoAndPlay(2);
    	i++;
    } 
    else {
    	stop();
    }
    
    trace(i);
    And this is code you should place in your first frame:

    Code:
    var i = 1;
    Try it out and let us know what happens. =)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center