Finally made some time to update my blog!!! I've been working hard and added lots of new classes to my library which i used for my own games. I'll be updating this site with new classes and examples over the coming months - so be sure to stay tuned for all the updates!
First on the list is the problem of timers and timing in general with Flash. Well not with flash as such, but with the web browsers. It seems that the flash player plugins for web browsers aren't very accurate at all when it comes to timing. You can ask flash in AS3 for any timer delay you like (from 1ms upwards). But the flash player will not deliver such small resolutions and even on larger delays, has a big variation between TimerEvents firing. I've written my own timing code, and implemented my own timers in AS3 and added them to my ever expanding library for anyone to use! Read on if you want to know how and why you would use these classes.
First of all, if your program needs ultra accuracy for timing them your going to disappointed. I've tested a few various methods i found on the net via google and i never found one that was any more accurate than the standard timers over a long period of time. You need to rethink your code, or work out ways to compensate for the inaccuracy. I've struggle with jerky movement/motion, inconsistent animation frame updates and problems using low resolution timing and now i'll share my thoughts and classes with you in the hope it helps others to avoid these problems and pitfalls. If you've ever added motion or animation or low resolution timed events to your AS3 code and then seen jerky playback in the player then read on - as i may be able to help!
I've created 2 classes for now, SuperStage and SuperTimer. SuperStage is a sprite replacement and should be used to replace the sprite class that becomes your main stage. This SuperStage calculates the delta time difference between frames and stores it in SuperStage.frameDelta (it's a static so you can reference it from anywhere) and it also calculates a smoothed time difference in SuperStage.frameDeltaSmoothed. This smoothed value is the milliseconds from the last frame with smoothing applied. This helps to iron out rough highs and lows in the actual frame time delta and is useful for smoothing visual animation like frame changes on animated sprites.
SuperTimer is a direct replacement for the Timer class and has a simple system that remembers the overflow when the timer fires (so that if a 1000 millisecond timer fires at 1050, the next timer will fire at 950 - or as close to that as possible). The class also lets you access the overflow time in superTimerObject.delayCurrent (not static, just a public var in each SuperTimer object) and you can handle it yourself and reset the value to zero in the event handler to make it work the same as a standard Timer. Aswell as knowing how much the timer was out by, you can also pause & resume the timer and it will remember the current milliseconds and pick-up exactly where it was paused. You can also pause all running timers, resume all paused timers and stop all running timers. The SuperTimer should work as an exact replacement for the standard Flash Timer class, and has all the basic functions, and can be used exactly like a normal timer without worrying about any difference.
Below is a small timer test (based on the TrailSprite demo from a previous blog entry). It shows the output and inaccuracy of all timers, and hopefully gives you an idea of how my two classes help you cope with the problem.
Mon, 08/25/2008 - 22:40
I'm not sure my smoothing timer will help if you need really accurate timing. Flash is limited to the hardware and the fps update of the browser (which is limited to 60 on most i believe). The only other way i know to do timing for sounds is by playing a silent sample which gives you a a ~46ms loop. Check out http://www.active-web.cc/html/research/f6sync/f6sync.txt. Good luck - lemme know how you get on!
Sat, 08/23/2008 - 05:02
I am trying to create an online mp3 sequencer on my website, I got horrible results, so I hope your library will help me!
Thanks a lot to make this code available!
Post new comment