ActionScript 3 QuickTip #4: Dynamic Frame Rates

This is a *very* cool new feature in ActionScript 3. We now have the ability to dynamically change the SWF frame rate at runtime using ActionScript. In the example below I have a somewhat creepy clown who is animating from side-to-side on the screen. You can use the slider to change the frame rate from 0 to 800 and something. There are a lot of applications for this such as lowering the frame rate for slower machines dynamically. The AS 3 code from the example is posted below:

  1. this.stage.frameRate = 0;
  2. rate.text = “0 fps”;
  3.  
  4. thumb.addEventListener(MouseEvent.MOUSE_DOWN, starter);
  5. thumb.addEventListener(MouseEvent.MOUSE_UP, stopper);
  6. thumb.addEventListener(MouseEvent.MOUSE_OUT, stopper);
  7. thumb.addEventListener(MouseEvent.MOUSE_MOVE, mover);
  8. function starter(args:Event)
  9. {
  10.         thumb.startDrag(false, new Rectangle(track.x, thumb.y, track.width, 0));
  11. }
  12. function stopper(args:Event)
  13. {
  14.         thumb.stopDrag();
  15. }
  16. function mover(args:Event)
  17. {
  18.         var dist:Number = (thumb.x – track.x) / (track.width + track.x);
  19.         this.stage.frameRate = Math.round(dist*1000);
  20.         rate.text = Math.round(dist*1000).toString() + ” fps”;
  21. }

Click on the image below to launch the example. Flash Player 9 required:

Lee


Commentary

  1. Shane Hoffa says:

    Very cool. Is there any way to change the fps without it effecting the speed but rather the smoothness of the animation? Example… If I’m using the Fuse Kit to animate the item from point A to point B within 2 seconds, Fuse will always take the same amount of time no matter what the fps is set to. It would just make the motion smoother.

  2. Otto says:

    Hey Lee, I really need you to tell me the name of the song you posted a long time ago in the part where you made the little linkin park balls move with the audio data from after effects.

    Thanks in advance

    -Otto

  3. Flo says:

    Hi Lee!
    Inspired by your ActionScript QuickTip I made an example (kind of simple slow-motion or fast-forward) using the Dynamic Frame Rate for an embedded video:
    http://www.video-flash.de/wp-content/uploads/2006/12/slowmotion-fastforward-demo.html

  4. Ben Nadel says:

    I haven’t done too much flash in a while (certainly no AS3 specific things) and this is a really sweet ass feature! Good to know. This alone might motivate me to put the ColdFusion on the slow track and bone up on AS3. Thanks!

  5. Ben Cline says:

    sweetness…..its like a dream come true.

  6. pixelmixer says:

    This is really cool, but it gets kind of disorienting moving back and forth without any motion blur going on. How would we add that in AS3? Does it work similarly to AS2 and bitmapdata objects with filters applied?

  7. lee says:

    Yes all of that functionality is in AS3. This example wasn’t meant to be visually smooth or anything. Just an example of how to change the FPS.

  8. sascha/hdrs says:

    It’s a nice new feature. What would be even more nice is to control to framerate on a per-movieclip base. But I guess the Timer will do that too while we don’t have that.

  9. sharon says:

    my pc keeps telling me to install flash players but they dont work right also when trying to watch a video on my pc i have pictures but no sound

  10. For the record, Fuse isn’t going to support Actionscript 3. Adobe brought in Robert Penner to work with them on creating a more intuitive tweening method for DisplayObjects than the old mx classes did. So I would expect to see some really cool things to come out of the final release of Flash 9!

  11. Very cool indeed! This will be great for dealing with slower machines.

  12. Haq says:

    gr8 idea.. thanx mate

  13. lus says:

    why is fuse better than zeh’s tweener?

  14. Samedi says:

    damn! this as3 thing seems pretty cool… I know that I must get into it sooner or later. Does someone know what percent of the users have the proper flash player?

  15. Thonbo says:

    i made a little something something to show that

    updateAfterEvent() renders ~10x fps at max
    and your fps will always cap at ~60 fps in browsers

    - read more inside

    http://www.thonbo.com/updateAfterEvent/updateAfterEvent.html

  16. Matt says:

    Is there any way this could be use with audio? Would it be possible to slow down a swf with an embedded audio file without altering the pitch?

  17. nitin says:

    can some one post the like of fla file for frame rate?
    thanks

  18. jana says:

    This is exactly the functionality I need.
    Is there a possibility to do this with a video WITH audio?

  1. Canadian Developers linked here

    [... This is a *very* cool new feature in ActionScript 3. We now have the ability to dynamically change the SWF frame rate at runtime using ActionScript. In the example below ...]

Leave a Comment