9 Replies Latest reply on Jul 22, 2008 10:51 PM by Christopher Barnard

    Executing code after a certain frame

    Christopher Barnard
      I am trying to get my MC to move on frame by frame from a particular starting frame in the movie. Anything below the stated frame I don't want anything to happen. I've tried this

      if (this._currentframe >= 6 )
      {
      trace ("more than 6")
      var frameGo = function () {nextFrame ();}
      var myTimer = setInterval(frameGo , 20000);
      }
      else if (_currentframe <= 5){
      trace ("less than 6")
      }

      This doesn't work. Using this code without the if/else statement:

      var frameGo = function () {nextFrame ();}
      var myTimer = setInterval(frameGo , 20000);

      Works but accross the whole movie,

      How do I get the code to execute after a certain point only?

      Help most appreciated!

      Chris
        • 1. Re: Executing code after a certain frame
          BuG300K
          I'm not too sure...Why not just put the code on the 6th frame of the movie clip?
          • 2. Re: Executing code after a certain frame
            MATTANDIE
            You pretty much have two options to accomplish what you want. The easiest is to call the function at frame the frame you want to start at . If that is an unnacceptable solution you will need 2 intervals not just one. something like this should work:
            • 3. Re: Executing code after a certain frame
              Christopher Barnard Level 1
              Hi guys thanks for the responses.

              It doesn't matter where I place this code on the timeline it executes across the whole of the movie:

              var frameGo = function () {nextFrame ();}
              var myTimer = setInterval(frameGo, 20000);

              This is fine for all frames above and including 6 - but if I'm back at the menu system from frames 1 - 5 (different sequences in the menu) - the code is STILL executing which I don't want to happen.

              Hence why I've been trying to implement and if/else statement. MATTANDIE I tried your code and it doesn't appear to solve the problem. It is advancing the playhead still across these first 5 frames.

              What I don't understand which may be part of the reason why this is not working is I can't trace the if statement - the output is not telling me that I'm at or above frame 6. The first two lines of code in this case in this instance are going to execute anyway despite what I declare in the if/else block. Perhaps I need to write the following so that the setInterval is incorporated in this block.

              var frameGo = function () {nextFrame ();}
              var myTimer = setInterval(frameGo, 20000);

              if (this._currentframe >= 6 )
              {
              trace ("more than 6")
              frameGo ();
              }
              else if (this._currentframe <= 5){
              trace ("less than 6")
              clearInterval(this.myTimer);
              }

              This may be what you were trying to get me to do MATTANDIE but it's not working for me. In your code example I notice your declaring the function frameGo () from frame 5 and below - I think I want it to do the opposite.

              Any more help appreciated :-)


              • 4. Re: Executing code after a certain frame
                MATTANDIE Level 1
                The code i provided works if you put it in the first frame of your movie. the first interval checks for the _currentframe to be less than 6. if you just want to start at a later frame, you can just use the second interval or a variation.

                I hope this helps.
                • 5. Re: Executing code after a certain frame
                  Christopher Barnard Level 1
                  Thanks for the response!

                  I played round with the code and no joy. I did as you had said - that first statement block reads:

                  if the playhead is at a frame number greater than or including 6 then execute the function goFrame ();. Accept that it appears that

                  var myTimer:Number = setInterval(frameGo, 20000)

                  Is executing anway in spite of it being part of a function that should only be called when the playhead reaches frame 6. This is what I kept finding when I was trying.

                  In the hope you can cast some more light I'm uploading a down and dirty FLA or what I'm trying to do. If you wait at the first frame the interval is executing - similar you can jump to the 6th frame - the interval works, which is great but if you return to the first frame the interval is still working!

                  FLA

                  Thanks for taking the time.
                  • 6. Re: Executing code after a certain frame
                    MATTANDIE Level 1
                    Ok, What i gave you was a pair of generic interval loops designed to play through a movie and adjust the movie speed once it hits frame 6 then play to the end at the new speed. With the way you have your file set up, you will have to make some modifications to get it to work the way you want it to. Start by adding a trace function to both intervals so you can see when its called. Then look at adding a clearInterval(frameGo); to either the timerStart function or your "return to menu" button functionality.

                    let me know if you still cant get it to work after you have tried these things.

                    • 7. Re: Executing code after a certain frame
                      Christopher Barnard Level 1
                      Struggling.

                      The problem is I can't rely on buton actions at the beginning of the movie to initiate any functions, because the button actions are coming from XML data strings - and that gets much too complicated for me at the moment!

                      It makes no sense to me (and curse my limited knowledege) that you can't use the if/else function thus:

                      if (currentframe >= 6 )
                      {
                      trace ("more than 6")
                      frameGo ();
                      }

                      The trace above never works which makes me think it is a scope issue? What amazes me further is that if the frameGo (); function is just that wrapped up in a function it appears to run anyway?

                      This is why I'm so confused.

                      Let me know if you can help me any further....

                      Chris



                      • 8. Re: Executing code after a certain frame
                        MATTANDIE Level 1
                        Ok Chris, I took a closer look at it over lunch, and with what you have set up putting this code on frame 6 will make it work. ** please note that once you go back to the first frame your interval will complete its loop but will not progress the frame, then it stops.

                        As to why you cant use just the if/else statement: The if/else statement by itself wont get called more than once unless it is in either an interval or an onEnterFrame function.
                        • 9. Re: Executing code after a certain frame
                          Christopher Barnard Level 1
                          Thanks so much for looking at that. I've been absent so excuse me for not getting back quicker!

                          Right that does work exactly how I wanted - thanks so much. And the code is logical for me to understand.

                          The only thing is that I notice when you jump to the first frame after 6, the interval seems random...i,e, it plays for only a few seconds then jumps to the next frame. When it goes to the next frame it traces currentframe - not when you first click.

                          That's really weird isn't it it isn't tracing the frame when it hits the first frame it is only doing it when it cycles to the next frame in the sequence?

                          Personally I can't see how that is possible?

                          You've done so much - so don't worry if that's enough for you...thanks for your continued help.

                          Chris