Is there a way to natively enable frame skipping on Flash?
When you develop a game, you develop animations to match the pace of the gameplay, and do it on a target frame-rate (usually 24-40fps for Flash). But if the user’s computer is too slow, and cannot maintain the target frame-rate, Flash will automatically lower the fps, making the application play slowly.
If you use a time based logic, the frame based rendering will not match the time based logic, and things will be weird and there’ll be a lot of corner cases to work around.
I know by fact that some games use this sort of frame skipping logic, such as Popcap’s Zuma Blitz. Are they implementing the frame skipping on their own?
I cannot afford to implement this so late in the project unless I can somehow reimplement the MovieClip class and make it easily frame skipabble. Also, wouldn’t the overhead of controlling the animations on your own (overwriting Flash native MovieClip control) be too much of an overhead?
Okay, I realize that you’re not looking for the following ‘solution’, which won’t work, as all frames will still be played:
The only solution is to just skip frames in your enterFrame callback:
(assuming, per best practices, you’ve centralized game logic/animation in a single callback), but that may present race conditions/complications when any asynchronous callbacks are run.
There is no way to actually skip frames at the runtime level; this is part of the contract of the AVM – no frames are skipped, all code is ran, no matter what. If performance is an issue, you might try asynchronous code execution. There are a couple of ways to do it:
1) Offload some computing to Pixel Bender, so that it can be processed asynchronously and/or in parallel (http://www.adobe.com/devnet/flex/articles/flashbuilder4_pixelbender.html)
2) Spread out the execution of lengthy operations over multiple frames (requires a state save and state restore, a la memento pattern). Details (and a great read) here: http://www.senocular.com/flash/tutorials/asyncoperations/
In any case, I’d (first and foremost) recommend definitively identifying the performance bottleneck with a tool like the Flash Builder Profiler or the Stats class (Mr. Doob).
Blitting might be the solution (create a spritesheet with a tile for each frame of the animation) as well. But in any case, I think what you’ll need to do is subclass MovieClip and override the play(), stop(), and gotoAndPlay() methods. Your class should look something like this:
Where the frame skip listener will skip frames according to the current frame rate or frame time, if necessary. Naturally, you’ll also need to remove the frameSkipListener when the animation has reached an end.
While the MovieClip override solution might do what you want on paper, if you have a lot of objects, this might actually degrade performance as the additional ENTER_FRAME listeners will add some overhead.