-
import com.actionsnippet.animation.*;
-
-
stage.frameRate = 60.0;
-
-
// args: main display object, Hz
-
var frim:QuickFRIM = new QuickFRIM(this, 60.0);
-
-
// start QuickFRIM internal loop
-
frim.start();
-
-
// listen for step and render
-
frim.addEventListener(QuickFRIM.STEP, onStep);
-
frim.addEventListener(QuickFRIM.RENDER, onRender);
-
-
// happes from 0 or more times per frame depending on your framerate and desired Hz
-
function onStep(evt:Event):void {
-
//
-
}
-
-
// happens once per frame after all STEP events have
-
// been dispatched
-
function onRender(evt:Event):void{
-
// number of times a step even was dispatched this frame
-
trace(frim.stepsPerFrame);
-
}
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...
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;