Reputation: 91
Ok, so I heard timer based movement is better than FR one. V = V + n per 1/60 sec, not per frame @ 60 fps. They said it makes the speed actual, not reduced with FR lag. But as I tried it, timer really isn't reliable at all. Am I doing something wrong? Cause Timer(20,0) goes slower as animation progresses. So there's no difference, if the clip gets overweighted, it slows down no matter timer based or FR? Yeah, I had a test clip that made new shapes every sec so there were quite a lot after a while but the timer really goes slower than my clock, sec gets longer. But timer is extra work to be done, more headache. So it's not worth it or did I use it wrong? (didn't make it prioritized before other functions?)
Upvotes: 4
Views: 2020
Reputation: 400
may be this will help.
public class Timer extends EventDispatcher
{
private var instance:int;
private var _interval:Number;
private var _updateInterval:uint;
private var _lastCount:int
private var _repeatCount:int;
private var _count:int;
private var _started:Boolean;
public function Timer(interval:Number,repeatCount:int=0)
{
_interval=interval;
_repeatCount=repeatCount;
}
private function checkTime():void
{
var curCount:int=int(currentTime()/_interval);
if(curCount<=_lastCount)return;
_lastCount=curCount
_count++;
dispatchEvent(new TimerEvent(TimerEvent.TIMER));
if(_count==_repeatCount&&_repeatCount>0)
{
stop();
dispatchEvent(new TimerEvent(TimerEvent.TIMER_COMPLETE));
_count=0;
}
}
public function start():void
{
if(_started)return;
_started=true;
instance = getTimer();
_updateInterval=setInterval(checkTime,10);
_lastCount=int(currentTime()/_interval);
}
public function stop():void
{
_started=false;
clearInterval(_updateInterval);
}
public function reset():void
{
_count=0;
stop();
start();
}
private function currentTime():Number
{
if (instance == 0)
{
instance = getTimer();
}
return (getTimer() - instance);
}
public function get currentCount():int
{
return _count;
}
}
Upvotes: 0
Reputation: 2254
You didn't do anything wrong. It's less than ideal, but Flash limits updates to a maximum frame rate, which means Timer
events aren't exact. Personally, I tend to make anything time sensitive using a frame based loop which checks the current system time using new Date().getTime()
, so I would use something like:
private var lastFrameTime:Number = new Date().getTime();
private var now:Number;
addEventListener(Event.ENTER_FRAME, frameTick);
private function frameTick(e:Event):void {
now = new Date().getTime();
V = V + n * (now - lastFrameTime) * 0.001;
lastFrameTime = now;
}
Upvotes: 1
Reputation: 429
Timers in AS3 are indeed unreliable. A timer in AS3 can not have a value lower than 1000. This was officially documented by Adobe. You can also make a test for a timer with 1000ms limit, and you'll notice that it will fire randomly at 800-999ms intervals, which is never accurate.
If you want to make a frame limited/free application, a better candidate is "getTimer". You can use the amount of ms passed to calculate the correct amount of time passed and then render a frame.
Look at the Clock class in this project: Realtime Clock
Upvotes: 2
Reputation: 18747
Yes, if your SWF is capable of rendering the stage in less than a frame's length, both are good, and if not, your framerate will suffer no matter what you do. You need to tune up your SWF for performance, if you want this big FPS. Also, the timer cannot fire its event faster than once per frame. (In fact I believe that frame-time approach is generally better, as you don't know if your SWF will run on a slow enough PC that won't be capable of competing with normal time flow otherwise.)
In your case, I'd drop timers, they throw one more event per frame, they are largely unreliable as their events are asynchronous, meaning you cannot consistently say that timer event will fire before the next frame, and with larger interval timers, a slightest lag of the host PC can spoil the fun to many players. (I remember Pyro vs my old PIII, which used system time to record level beat times, but you couldn't speed it up once you fire that ball, and the animation was heavy enough to drop frame rate, essentially preventing me to get fast time scores.) Your timer is already firing its event about once per frame, and cannot get faster.
Upvotes: 4