Hello all.
I wanted something as the title sais but there wasn't any Starling way to make it easy so I added my own, and since I've seen people asking for it I thought sharing.
I've also published it in my blog. Just to advertise it here it is http://konsnos.wordpress.com/2012/03/11/smooth-movement-in-starling/ 😜
You will need these three variables.
/**
* The time between the previous and the present frame in milliseconds.
*/
private var deltaTime:Number;
private var prevFrame:Date; // The time of the previous frame rendered.
private var nextFrame:Date; // The present time.
In your constructor add added to stage listener just to have access to the stage and add the enter frame listener.
addEventListener(Event.ADDED_TO_STAGE, init);
Calculate the time of the first frame.
private function init(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
stage.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(TouchEvent.TOUCH, fire); // Mouse event.
prevFrame = new Date();
}
And voila. Just pass the deltaTime variable anywhere you want. I personally found it very useful. 🙂
private function update(e:Event):void
{
nextFrame = new Date();
deltaTime = (nextFrame.time - prevFrame.time) / 1000;
// Do your stuff here.
prevFrame = nextFrame;
}
Thanks for the great library 😉