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

Thursday, July 9, 2009

AS3 Timer accuracy

Thursday, July 9, 2009   

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.

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:

 
 Return to the main page
Comments:

There are currently 9 Comments:

Blogger Goose said...
“This might have to do with the frame rate of your SWF. There is probably a way to increase the frame rate in your main AS file with the [SWF] meta data tag.”
 
Blogger e.dolecki said...
July 9, 2009 6:06 PM
“I always run 31fps. I'd think a Timer would just work regardless of what frame rate one is running.”
 
Blogger renaun said...
July 9, 2009 6:24 PM
“Take a look at this post:

http://www.bit-101.com/blog/?p=910”
 
Blogger e.dolecki said...
July 9, 2009 6:33 PM
“renaun,

Thanks for that link. Good reading there. So in essence there really isn't a great way of doing this, unless... I just had a thought...

a Timer that could dynamically adjust it's delay to compensate. That would mean the getTimer could help a bit.

My reducing by 3ms is working, just not sure how dirty it makes me feel. Since it's working, it should be okay, right? I'm not releasing it or anything, it's for internal use.”
 
Anonymous Gritfish said...
July 9, 2009 7:27 PM
“I ran into this problem myself a while back.

The timer class is a little inaccurate, basically because it fires on the frame after the timer goes off, then starts again from there. This means:
The closer you have your timer events, the less accurate it becomes.
The lower your framerate, the less accurate it becomes.

I don't have the full source code on me, but the basic idea is to:
1) create a Date variable
var d:Date = new Date()

2) on your timer listener, check create a new Date, and subtract the first date from it
newDate.valueOf() - d.valueOf()

3) this will give you the millisecond difference from when you started the timer.

4) you then update the timer length to adjust for the difference between what it should be and what it is.

My blog is at http://blog.gritfish.net

I'll try and put up a full solution in the next week, but I hope this gets you looking in the right direction.”
 
Anonymous Gritfish said...
July 12, 2009 9:20 PM
“Saw you'd abandoned the system clock idea, but I thought I'd comment here anyway and let you know I've put up a class that's a fair bit more accurate than the timer class (500 ticks in 5 secs with +/- 20ms total). Thought you might still be interested.

http://blog.gritfish.net/?p=76”
 
Blogger e.dolecki said...
July 13, 2009 8:38 AM
“Hey Gritfish,

I'm going to play with this class a little bit and see if using it makes a big difference - I believe that it might. It's probably good practice to be as accurate as possible anyway.

Thanks for making that class public for the whole community's benefit!”
 
Anonymous Riccardo Bartoli said...
September 17, 2009 11:07 AM
“Hi Eric,
did this class help you to obtain a more accurate timer firing?

Cheers”
 
Blogger e.dolecki said...
September 19, 2009 4:40 PM
“Yes, it makes a big difference.”
 
 Leave a comment