Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Monday, July 13, 2009

AS3: TrueTimer from Gritfish

Monday, July 13, 2009   

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).
var myTimer:TrueTimer = new TrueTimer( 100, 100 );
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 );
}
And here is the base class itself:
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: ,

 
 Return to the main page
Comments:

There are currently 1 Comments:

Blogger e.dolecki said...
“I've actually implemented this and using a stopwatch it's nearly dead on. Of course it's not going to ever be 100% accurate for a number of reasons, but this is A LOT closer than previous attempts at accuracy.

Because I was previously using a magic number in my solution soup, it was only ever guaranteed to run like that on a certain computer. This solution negates the need for a hack like that.

This is really sweet, thanks Gritfish.”
 
 Leave a comment