I'm starting to see a trend around the Flash community about time based animation in Flash and members stating that setInterval is the equivalent of time-based animation. This is incorrect so here is where I answer the questions once an for all.SetInterval is called on an Event.ENTER_FRAME event if it has expired. This means that a target client running at 5fps has the potential to loose 19ms ( or even more with application lag) per frame. The real concept is to actively look at how much time has passed between the last frame update and the current frame update, we will then have a figure to use to do our time based math, so here is the code:
package {
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.utils.getTimer;
import flash.utils.Timer;
import flash.events.TimerEvent;
/**
* ...
* @author Gary Paluk
*/
public class TimeBasedAnimation extends Sprite {
[Embed(source = '../lib/sprite_sheet1.png')]
private var m_SpriteSheet: Class;
private var m_Timer: Timer;
private var m_UpdateInterval: int;
private var m_sprite: DisplayObject;
public function TimeBasedAnimation( ) {
m_Timer = new Timer( 20 );
m_Timer.addEventListener( TimerEvent.TIMER, onTimer );
m_Timer.start( );
m_sprite = new m_SpriteSheet( );
addChild( m_sprite );
}
private function onTimer( event: TimerEvent ): void {
var totalInterval: int = ( getTimer( ) - m_UpdateInterval ) ;
// move the sprite at 50 pixels per second
m_sprite.y += 50 * ( totalInterval / 1000 );
m_UpdateInterval = getTimer( );
}
}
}
Gary Paluk
0 comments:
Post a Comment