I have created stopwatch which consist of two classes. First is stopwatchModel. Second is stopwatchView. My stopwatch is working but it is showing less time than how much time it last for real. It looks like my stopwatch is slower than reality.
StopwatchModel
private function initStopwatchModel():void{
timer = new Timer(100,0);
timer.addEventListener(TimerEvent.TIMER,onTimer);
}
private function onTimer(e:TimerEvent):void{
decsec++;
if (decsec == 10){
sec++;
decsec=0;
if (sec == 60){
sec = 0;
min++;
}
}
playerTimeString = min + ":" + sec + ":" + decsec + "0";
dispatchEvent(new Event("NEW_TIME"));
}
StopwatchView
private function initModelEventListeners(){
_model.addEventListener("NEW_TIME",onNewTime);
}
//update textoveho pola, vzdy ked sa zmeni cas
private function onNewTime(e:Event):void{
textFieldStopWatch.text = _model.playerTimeString;
}
Thank you for answer
Because timer is not fully accurate but it is somehow dependent on the frame rate so it finds the closest interval that is multiple to one frame time.
I would recommend you to save getTimer() return using http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/package.html#getTimer() when the application starts and then measure it on timer and calculate the difference – this will work for sure.
The problem is also documented at http://www.computus.org/journal/?p=22