Monday, July 13, 2009
AS3: TrueTimer from Gritfish
Monday, July 13, 2009
1 Comments
Gritfish's TrueTimer class is something that attempts to tackle the inaccuracy of the built-in Timer class. Run over and read his post and see the class, or you can check it out here as well. I have not yet implemented this in anything, it seems that when you receive a Timer event, you're going to have to read the classes public variables instead of getting them from the Timer event object itself. Which is fine I guess.

A quick use example (make sure your listener method is using type of Event and not TimerEvent though - I ran into that myself for a minute).

A quick use example (make sure your listener method is using type of Event and not TimerEvent though - I ran into that myself for a minute).
var myTimer:TrueTimer = new TrueTimer( 100, 100 );And here is the base class itself:
myTimer.addEventListener( TimerEvent.TIMER, onMyTimer );
function onMyTimer( e:Event ):void {
//you can't use the e.currentTarget.currentCount or anything like that
trace( myTimer.currentCount );
}
package {
import flash.display.*
import flash.events.*
import flash.utils.*
public class TrueTimer extends MovieClip {
public var delay:int
public var repeatCount:int
public var initTime:Date
public var currentTime:Date
public var currentCount = 0
public var __offset:int = 0
public var running:Boolean = false
public var timeFromStart = 0
public var timeShouldBe = 0
public function TrueTimer(DELAY,REPEAT){
delay = DELAY
repeatCount = REPEAT
initTime = new Date()
currentTime = initTime
}
public function evaluateTime(e:Event){
var now = new Date()
var msDiff = now.valueOf() - currentTime.valueOf()
__offset += msDiff
currentTime = now
if(__offset > delay){
while(__offset > delay){
currentCount ++
__offset -= delay
if(repeatCount != 0){
if(currentCount == repeatCount){
timeFromStart = now.valueOf() - initTime.valueOf()
timeShouldBe = (repeatCount*delay)
dispatchEvent(new Event(TimerEvent.TIMER))
dispatchEvent(new Event(TimerEvent.TIMER_COMPLETE))
__stop()
}else if(currentCount < repeatCount){
timeFromStart = now.valueOf() - initTime.valueOf()
timeShouldBe = (repeatCount*delay)
dispatchEvent(new Event(TimerEvent.TIMER))
}
}else{
timeFromStart = now.valueOf() - initTime.valueOf()
timeShouldBe = (repeatCount*delay)
dispatchEvent(new Event(TimerEvent.TIMER))
}
}
}
}
public function __start(){
initTime = new Date()
currentTime = initTime
running = true
addEventListener(Event.ENTER_FRAME,evaluateTime)
}
public function __stop(){
running = false
removeEventListener(Event.ENTER_FRAME,evaluateTime)
}
public function __clear(){
currentCount = 0
__offset = 0
running = false
removeEventListener(Event.ENTER_FRAME,evaluateTime)
}
}
}
Labels: AS3, Timer Class
Thursday, July 9, 2009
AS3 Timer accuracy
Thursday, July 9, 2009
9 Comments
I've just bit into some weirdness. I have a Timer that I want to fire every 100th of a second. So it would be new Timer( 10, x ); However when testing this isn't not accurate (I am printing the time to the screen). So if I drop the Timer down to fire every 7ms instead of 10, it aligns with my watch, my computer, and my stop watch on my iPhone.
That's great, but what when I move this to another machine? It will probably be slightly off.
What is even stranger is that when I do new Timer( 7, 1800 ); - it still reaches 18 seconds, even though it should have fired a complete before that.
Is there a really accurate way of timing something as close to possible? I know this is a pretty old topic, maybe someone has found something really reliable.
[July 10th Update]
I have decided to fire on every 10th of second. I'm losing a bunch of timing control, but it wasn't accurate enough anyway. Instead of working on reading the system clock and making dynamic adjustments on the fly, I've decided that increasing the delay of the timer will work as it's now only off a tiny bit compared to before. Of course the longer it runs, the more it will be off, but my timer isn't going to run for a very long time, thankfully.
That's great, but what when I move this to another machine? It will probably be slightly off.
What is even stranger is that when I do new Timer( 7, 1800 ); - it still reaches 18 seconds, even though it should have fired a complete before that.
Is there a really accurate way of timing something as close to possible? I know this is a pretty old topic, maybe someone has found something really reliable.
[July 10th Update]
I have decided to fire on every 10th of second. I'm losing a bunch of timing control, but it wasn't accurate enough anyway. Instead of working on reading the system clock and making dynamic adjustments on the fly, I've decided that increasing the delay of the timer will work as it's now only off a tiny bit compared to before. Of course the longer it runs, the more it will be off, but my timer isn't going to run for a very long time, thankfully.
It still sucks, and while I can't expect any timer to be absolutely accurate for numerous reasons, I'd hope that in the future we're able to get something that is more precise, somehow, someway. FP11?
Labels: Timer Class