Monthly Archives: September 2009

QuickFRIM

Actionscript:
  1. import com.actionsnippet.animation.*;
  2.  
  3. stage.frameRate = 60.0;
  4.  
  5. // args: main display object, Hz
  6. var frim:QuickFRIM = new QuickFRIM(this, 60.0);
  7.  
  8. // start QuickFRIM internal loop
  9. frim.start();
  10.  
  11. // listen for step and render
  12. frim.addEventListener(QuickFRIM.STEP, onStep);
  13. frim.addEventListener(QuickFRIM.RENDER, onRender);
  14.  
  15. // happes from 0 or more times per frame depending on your framerate and desired Hz
  16. function onStep(evt:Event):void {
  17.      //
  18. }
  19.  
  20. // happens once per frame after all STEP events have
  21. // been dispatched
  22. function onRender(evt:Event):void{
  23.      // number of times a step even was dispatched this frame
  24.      trace(frim.stepsPerFrame);
  25. }

I have been using this QuickFRIM class for the past few days and have been pretty happy with it. Being someone who still occasionally likes to roll their own AS animation... with Zeno's, Hooke's, Polar Coordinates etc... Having a simple solution for frame-rate independent motion is nice... especially for small games... I used to have a more complex technique for this that was significantly harder to maintain. The above is just the basic setup for using the class.

The most important part of the class is the Hz (Hertz) argument in the QuickFRIM constructor. This should be set to how many times you want your step event to be dispatched per second. Usually this would simply be set to your target fps... so if your swf fps is 30, you should probably pass 30 to the QuickFRIM constructor. If you were to have an fps of 30 and pass 60Hz to your constructor... the step event would consistently dispatch 2 times per frame. If you had an Hz of 10 and an fps of 30, your step even would only get triggered every three frames etc...

Here's a demo...

I should note that I spent some time looking at different FRIM implementations online and in the end I found this thread https://developer.playfirst.com/node/860 which my implementation is significantly based upon.

Here's the code for the class
Here you can download a zip of the class

... and here is some very quick documentation
QuickFRIM:

/*
@param main - the main timeline or other DisplayObject
@param hz - Hertz for he timeStep
*/
new QuickFRIM(main:DisplayObject, hz:Number = 60.0)

// dispatched every time a step even occurs
QuickFRIM.STEP

// dispatched after all step events have occurred at the end of the frame
QuickFRIM.RENDER

// start dispatching events
public function start();

// stop dispatching events
public function stop();

// 1.0 / Hertz
public var timeStep:Number;

// total number of time steps that have been dispatched
public var totalTimeSteps:Number=0;

// numer of step events dispatched this frame
public var stepsPerFrame:int=0;

// boolean to disable FRIM
public var disable:Boolean = false;

Posted in motion | Tagged , , | Leave a comment

Great ActionScript Site

I keep meaning to post a link to this site... really great articles over there.... I highly recommend reading through them...

http://jacksondunstan.com/

After reading Everything's a Function. I remembered another strange thing that I noticed about the ActionScript compiler and auto-formatting.

Auto-formatting in flash is totally broken and can completely ruin your code - so never use it. It's officially unusable as far as I'm concerned. I can dig up some code later and add it here... most people reading this probably know exactly what I'm talking about...

Anyway... one thing I've seen auto-formatting do is to remove the () after a class instantiation...

From this:

var s:Sprite = new Sprite();

to this:

var s:Sprite = new Sprite;

... and you would think the second one would break, but it doesn't - it works just fine.... very odd... to see it in action try this:

Actionscript:
  1. var s:Sprite = new Sprite;
  2. with(s.graphics) beginFill(0xFF0000), drawCircle(0,0,500);
  3. addChild(s);

Can anyone shed light on that?

Posted in links | Tagged , , , | 10 Comments

Processing Class at FMA

This Sunday I'm doing a free one day workshop in Processing.

Processing is a gateway programming language. It helped me to ease my way into Java, C++ and OpenGL. Without Processing, learning those languages would have been much harder for me. Processing is also an amazing tool for prototyping and doing proof of concept tests.

The class itself is eleven weeks long but the first class is free. If you'd like to read more about it, or sign up for the whole eleven weeks check out this link:

Processing Class at FMA

If you want to attend the free workshop, simply post a comment and you'll be e-mailed a free pass for the class.

And if you haven't seen Processing.js yet... check it out here.

Posted in Uncategorized | 4 Comments

Timebased Animation Idea (FRIM & QuickBox2D)

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2.  
  3. // change the frame rate and test...
  4. stage.frameRate = 60;
  5.  
  6. [SWF(width = 800, height=600)]
  7.  
  8. var sim:QuickBox2D = new QuickBox2D(this);
  9.  
  10.  
  11. var circle:Shape = Shape(addChild(new Shape()));
  12. with(circle.graphics) beginFill(0), drawCircle(0,0,50);
  13. circle.x = 100, circle.y = 100;
  14.  
  15.  
  16. sim.start();
  17.  
  18. var startTime:Number;
  19. // delay the start a bit
  20. setTimeout(function():void {
  21.   startTime = getTimer();
  22.   sim.addEventListener(QuickBox2D.STEP, onTimeStep);
  23. }, 500);
  24.  
  25. function onTimeStep(evt:Event):void{
  26.    
  27.     circle.x += (600 - circle.x) / 12;
  28.    
  29.     if ((600 - circle.x) <1.5){
  30.         trace("frameRate: ", stage.frameRate);
  31.         trace("totalTime: ", getTimer() - startTime +" ms");
  32.         trace("totalTimeSteps: ", sim.totalTimeSteps);
  33.         sim.removeEventListener(QuickBox2D.STEP, onTimeStep);
  34.     }
  35. }

Note: This snippet requires QuickBox2D 1.0 or greater

Really not sure how I didn't think of doing this before last night... I realized that the simple FRIM code inside of QuickBox2D can be used to make any standard frame-based style ActionScript animation time-based. In this snippet I do a simple ease out one liner:

circle.x += (600 - circle.x) / 12;

Code like that on an enterFrame would run at different speeds depending on your framerate. However, if you run it on QuickBox2D's STEP event it runs independent of fram-erate. I'm obviously going to just wrap this up into it's own mini-library separate from QuickBox2D and do a more in depth post about it... but for now, this snippet just traces out the results. These are my results from my macbook pro dual 2.4 intel....

frameRate: 12
totalTime: 1165 ms
totalTimeSteps: 93

frameRate: 24
totalTime: 1162 ms
totalTimeSteps: 93

frameRate: 30
totalTime: 1166 ms
totalTimeSteps: 91

frameRate: 60
totalTime: 1167 ms
totalTimeSteps: 93

frameRate: 120
totalTime: 1168 ms
totalTimeSteps: 91

Anyway... will peel the FRIM code out of QuickBox2D and wrap it up in its own library sometime soon...

Posted in motion | Tagged , , | Leave a comment