5 Replies Latest reply on Mar 19, 2007 4:19 PM by kglad

    How not to reregister callbacks when movie loops?

    dareff
      I am new to AS and Flash, so excuse this basic question...

      I am trying to create a single frame application that simply shows some pictures (one at a time). The picture loads are controlled either by (a) a button being pressed or (b) a timer.

      I have (a) working fine, but (b) is a little bit of a problem.

      From reading the documentation on the Flash timer (setInterval and clearInterval) it appears that my movie must be at least 2 frames, since the documentation says that even if the timer time expires, it also waits until the next frame is entered.

      Well, I tried simply adding a 2nd frame. Apparently that is not the correct answer, since every time I press a button, the trace is showing that the button event listener function is being registered over and over again... For example, the first time I hit the button, it calls the button callback function once. The second time, it calls the button callback function more than once (i.e. 2+ times).

      How do I stop these multiple registrations of the event listeners from happening, so I can actually use the timer functionality?

      Thanks in advance.
        • 1. Re: How not to reregister callbacks when movie loops?
          Level 7
          dareff,

          > From reading the documentation on the Flash timer (setInterval
          > and clearInterval) it appears that my movie must be at least 2
          > frames, since the documentation says that even if the timer time
          > expires, it also waits until the next frame is entered.

          I'm curious what line of the docs suggested this to you. setInterval()
          functions just fine in a single-frame SWF. For example, check out this
          basic sample ...

          var i:Number = 1;
          var id:Number = setInterval(test, 500);
          function test():Void {
          if (i++ == 3) clearInterval(id);
          trace("Test successful.");
          }

          In a single-frame SWF, you'll see the test message appear three times,
          once every half second. After that, it stops. Run that once in the default
          framerate (12fps), then again at 1fps. Same behavior. No frames are waited
          for. :)


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


          • 2. Re: How not to reregister callbacks when movie loops?
            dareff Level 1
            Thanks, your example does work for your time setting, however I am setting my timeout for around 5+ seconds, which doesn't allow me (from a usability standpoint) to set the frame rate high enough for my application.

            I should have been a little more specific :)

            from the help file:
            "If interval is greater than the SWF file's frame rate, the interval function is called only after interval has expired and the playhead has entered the next frame; this minimizes the impact each time the screen is refreshed."

            However, your response did get me to something workable for my 5 second timer -- so thanks!

            var i:Number = 1;
            var loops:Number = 0;
            var id:Number = setInterval(test, 500);
            function test():Void {
            trace("time: " + ((loops*5)+(i*0.5)) + " seconds passed");
            if((i++)>=11)
            {
            i = 1;
            loops++;
            clearInterval(id);
            id = setInterval(test, 500);
            trace("do 5 second interval action here");
            }
            }
            • 3. Re: How not to reregister callbacks when movie loops?
              kglad Adobe Community Professional & MVP
              as david said there doesn't have to be a next frame. (for example, an onEnterFrame handler executes repeatedly at (close to) the frame rate whether there's more than 1 or only 1 frame in your swf.

              with setInterval(), the interval actually can (and does) execute more frequently than the frame rate (if the frequency is high enough). it's just that the stage isn't updated until the playhead enters (or is due to enter) the next frame. (unless you use updateAfterEvent() in the function called by setInterval(). )

              and that statement only applies the intervals the are executed MORE frequently than the swf's frame rate.

              your interval frequency is LESS than any possible frame rate.

              you can use:

              • 4. Re: How not to reregister callbacks when movie loops?
                dareff Level 1
                Thanks. I have no idea why it didn't work the first time I tried something very similiar to both your answers. Thanks again.