views:

37

answers:

2

I have built an online game system that depends on a timer that records how long it took a player to complete a challenge. It needs to be accurate to the millisecond. Their time is stored in a SQL database.

The problem is that when I use the Timer class, some players are ending up getting scores in the database of less than a second. (which is impossible, as most challenges would take at least 11 seconds to complete even in a perfect situation.)

What I have found is that if a player has too many browser windows open, and/or a slow computer, the flash game slows down actually affecting the timer speed itself. The timer is 'spinning' on screen so you can physically see the numbers slowing down.

It is frustrating that I cannot just open a second thread or do something to allow flash to keep accurate time regardless of whatever else is going on in the program. Any ideas?

A: 

Timer is (in theory) independent of framerate, so hopefully should execute "on time" even if the player slows the framerate (in heavy display update cases). Of course, Timer is still dependent somewhat on CPU load and will have some marginal inaccuracies. 10+ second inaccuracies? I doubt it. I think either

a) You are using an incorrect number for the timer counts (timer runs on milliseconds, not seconds). If you ran it every 50ms, you could calculate total time based on the number of ticks it got through * 50ms

b) Your users are using something like Tamper Data to pause the request and change their "score"

c) You've got another bug in the game that's causing the issue.

Impossible to tell without sample code. Got any?

Typeoneerror
Here is a general idea of what is happening:var timer:Timer = new Timer(1);timer.addEventListener(TimerEvent.TIMER, updateTimer);function updateTimer(event:TimerEvent):void{ var time:String = PadZero.convert(event.target.currentCount,6); digit[0].text = time.charAt(0); digit[1].text = time.charAt(1); digit[2].text = time.charAt(2); digit[3].text = time.charAt(3); digit[4].text = time.charAr(4);}PadZero.convert is a function that pads zeros to the left of a supplied number. the 'currentCount' is what I end up storing. It slows down with load.
Nuthman
Yeah, firing and responding to an event every millisecond is really going to slow down the player. Just try tracing out currentCount and you'll see it won't even make it to 1000ms in 1 second. You're better off just getting the time at the start and end as @frankhermes suggested.
Typeoneerror
+2  A: 

Another way of tracking time is using getTimer() at the start of the game. Store the result in a variable. Another getTimer() call at the end of the game will let you calculate the amount of milliseconds that the game lasted.

frankhermes