Weird, huh? I was recently building an application for AIR that needed a simple timer. No problem, right? ActionScript has a nifty little Timer class I can use. So I built the application around this timer on my Mac, which has a 3.0 Ghz Intel Quad-core. I tested it, and everything seemed fine. I wasn’t quite finished, though, and I had to leave on a weekend trip, so I exported the project and put it on my Dell 1545 laptop. Before I recompiled, I ran it on the laptop. The clock ticked 4 times as fast. I was pissed. And confused. I had heard that the AS3 Timer is based on framerates, but I didn’t check for myself, and figured it would probably be negligible anyways. Not the case. Don’t believe me? Check out this Timer class example from computus.org. Hint: Try right-clicking the timer, waiting, then clicking outside the timer. The delay racks up while the movie is paused.
So, I contacted Adobe, and Mike Potter suggested I use the Date class instead. He didn’t say how, but I deduced that the date must be coming from the system clock rather than some Flash Player internal math. So, I decided to try a new strategy, creating a StopWatch class that could:
- Get the date as a Unix-style timestamp
- On each frame, get a new timestamp
- Subtract the starting timestamp from the latest timestamp
- Parse the result into hours, minutes, and seconds then update a label with it
- Repeat
It worked. Brilliantly (thanks Mike!). So, for anyone out there who is facing the same problem, you can check out this sample app and view/download the source.
8 Responses