7 Replies Latest reply on Apr 17, 2007 11:07 AM by Newsgroup_User

    using setInterval() to execute animation faster than  framerate

    Level 7
      Earlier we discussed making an animation movie clip independent of frame
      rate and used the following code in the first frame of a movie clip (call it
      "mc"):

      var interval_between_frames=100;
      var interval_between_frames_reference;
      if (interval_between_frames_reference)
      {
      clearInterval(interval_between_frames);
      }
      interval_between_frames_reference = setInterval(this, "nextFrame",
      interval_between_frames);
      stop();
      and put stop(); into the rest of the frames of mc.

      However this work one direction only - it can slow down the speed of the
      animation below the framerate, but it cannot increase it above the frame
      rate.

      I.e. if in my FLA file I have an mc with the above code and in the main time
      line I place
      attachMovie("mc","mc",depth)
      and set the framerate of my FLA to 1, the mc executes at 1 fps
      irrespective of value of interval_between_frames.

      Why is that and how can I use setInterval() to execute faster than
      framerate?


        • 1. Re: using setInterval() to execute animation faster than  framerate
          Greg Dove Level 4
          you need an updateAfterEvent at the end of your setInterval function call, otherwise screen updates only occur at the movie's frame rate. Try this.
          • 2. Re: using setInterval() to execute animation faster than  framerate
            Level 7
            Yes, this way I can control the speed both above and below fps. But there
            two things of concern:
            1. the speed still seems to depend directly on the containing FLA fps. From
            what I see it looks like the resulting frame rate is something like
            1000*fps/interval_between_frames

            2. the above dependance takes place only within certain values.
            For example, if fps==1 then the speed increases while
            interval_between_frames goes down to 100 or so. Futher reduction of
            interval_between_frames does not seem to affect the animation speed.

            How would you comment on that?

            "GWD" <webforumsuser@macromedia.com> wrote in message
            news:evt9u0$kir$1@forums.macromedia.com...
            > you need an updateAfterEvent at the end of your setInterval function call,
            > otherwise screen updates only occur at the movie's frame rate. Try this.
            >
            > var interval_between_frames=100;
            > var interval_between_frames_reference;
            > if (interval_between_frames_reference)
            > {
            > //make change here as well:
            > clearInterval(interval_between_frames_reference);
            > }
            > interval_between_frames_reference =
            > setInterval(this,"advanceFrame",interval_between_frames);
            > function advanceFrame(){
            > this.nextFrame()
            > updateAfterEvent()}
            > stop();
            >


            • 3. Re: using setInterval() to execute animation faster than  framerate
              Rothrock Level 5
              The time between function calls is only a "suggestion" to Flash. It is dependent upon the framerate of the movie. It is never exactly what you set it at and will vary even a bit just from call to call.

              If you are trying to achieve some kind of tight control of framerate in Flash you are wasting your time. If you are just trying to understand how things work then you are doing just fine.

              Personally I usually publish all my files at one rate – unless I have a very specific reason for not doing so. That way any swf plays well with any other if I need to combine them later. You can use tricks like onMouseMove and setInterval to "speed" up the responsiveness of specific tasks where the user is likely to have attention focussed.

              AND EVEN BETTER you can use setInterval for things that don't need to happen so often. Remember that with Flash less is often more.
              • 4. Re: using setInterval() to execute animation faster than  framerate
                Level 7
                Thanks,
                I am trying to understand how things work in Flash to see what sort of
                control of framerate I can command.

                So you are saying that a mover clip's apparent framerate is dependent on
                fps as I described earlier? If this is the limit of control - let's accept
                the reality.

                I looked at Windows Task Manager and could not detect a difference in CPU
                load and memory consumption if I make an object move "naturaly" according to
                fps or use setInterval - is this a correct assumption?

                "Rothrock" <webforumsuser@macromedia.com> wrote in message
                news:evtgle$rvu$1@forums.macromedia.com...
                > The time between function calls is only a "suggestion" to Flash. It is
                > dependent upon the framerate of the movie. It is never exactly what you
                set it
                > at and will vary even a bit just from call to call.
                >
                > If you are trying to achieve some kind of tight control of framerate in
                Flash
                > you are wasting your time. If you are just trying to understand how things
                work
                > then you are doing just fine.
                >
                > Personally I usually publish all my files at one rate ? unless I have a
                very
                > specific reason for not doing so. That way any swf plays well with any
                other if
                > I need to combine them later. You can use tricks like onMouseMove and
                > setInterval to "speed" up the responsiveness of specific tasks where the
                user
                > is likely to have attention focussed.
                >
                > AND EVEN BETTER you can use setInterval for things that don't need to
                happen
                > so often. Remember that with Flash less is often more.
                >


                • 5. Re: using setInterval() to execute animation faster than  framerate
                  Greg Dove Level 4
                  I too publish all my movies at one rate. I choose 30 fps. Couldn't say why though. But it achieves the same sort of consistency that Rothrock mentioned.

                  I can't imagine myself wanting to use the setInterval function to try to move the playhead in the way you are trying to achieve. It is possible and could be useful (although I've never needed to do it) to tween something more smoothly in a low fps movie (there are side by side examples of this in online tut's). But to achieve this you would alter the movieclip's properties - _x, _y etc, not try to move its playhead to speed up a timeline tween. And you need to call updateAfterEvent() each time, otherwise even if the properties are changed, you won't see it until the visuals update at the speed of the frame rate.
                  • 6. Re: using setInterval() to execute animation faster than  framerate
                    Rothrock Level 5
                    My framerate of choice is 21 – back in the day 21 was a magic frame rate that worked better on the Mac platform than say 20 or 24. I've actually been toying with the idea of jumping to 31. Another rate that was magic in the day. I don't know if those rates are still magic, but they are also in good ranges anyways.

                    As for the CPU usage and all, I can't really tell you without seeing exactly what you are doing. But let me tell you a little story.

                    A poster here had a grid of little dots movieclips, maybe about 400 or so. Each dot would figure out how far it was from the mouse and inversely adjust its scale, i.e. closer dots were bigger and further dots were smaller. They were doing the calculating with an onEnterFrame. So for a framerate of 30, that was about once every 33 milliseconds.

                    The problem was it was kind of clunky and the scaling would flow smoothly for a bit and then jumpy for a bit, and kinda all over the place.

                    By moving the calculate distance and set scale routine called with a setInterval we discovered that a call about every 50 milliseconds or so enabled it to react smoothly.

                    Less is more. Remember that regular film movies play at 24 fps and TV is more or less 30.
                    • 7. Re: using setInterval() to execute animation faster than  framerate
                      Level 7
                      Thanks, I'll think of it. As to your referenve to TV - IO have always been
                      wondering why with a pathetic resolution and framerate TV motion pistires
                      look much better then the computer ones? If I wanted an object to cross the
                      screen in say 3 sec I need to ste framerate to 60 to make the movement
                      smooth and to avoid ghost effect