Flash constant frame rate

A common problem in flash games, especially if there are some heavy graphics effects, is to handle the frame rate correctly. Even if it is possible to set a desired frame rate, the flash player will only try to keep it, but the actual frame rate may vary.
I’ve googled for some way to solve this problem, and I’ve found two different solutions.

One is to set the fps to somethings high, like 100fps, and then try to slow down the frame rate to the desired value with a busy-loop:
[as] starttime = newDate()
var currtime=new Date()
var ttime=currtime-starttime
while(ttime<1000/DESIREDFRAMERATE){ var currtime = new Date() var ttime=currtime-starttime } starttime=new Date() } [/as] In this way, however, some flash player's activities like garbage collection will be slowed down too, and this often lead to browser crashes. This is better explained in an interesting comment in a post on the Andrè Michelle blog by the user Troy Gilbert.

So the better way is to set the fps to something high, and then check when the interval 1000/frames_per_seconds is lapsed:
[as] function onEnterFrame() {
var dtime:Number = getTimer() – this.lastFrameTime;
if(dtime > (1000/FRAMERATE)) {
var skipCount = 0;
while(dtime > (1000/FRAMERATE) && skipCount < MAX_FRAME_SKIP) { skipCount++; this.onEnterRealFrame(); this.lastFrameTime += 1000/FRAMERATE; dtime = getTimer() - this.lastFrameTime; } this.lastFrameTime = getTimer(); } } [/as] onEnterRealFrame has the game logic, for example players movement and MAX_FRAME_SKIP prevent the player to hang too much.

We check if the time of a frame (1000/FRAMERATE) is lapsed, so we call onEnterRealFrame. We will call it how many times as the number of frames skipped from the last onEnterRealFrame call.
For example, if the fps is 10, and our game has a slow down for 300ms, it will call onEnterRealFrame for 3 times (300/(1000/10)) to recover the time lost.

However this solution has a problem: animated MovieClips will run out of sync. This can be solved either using tween engines like Zigo, or using a sound clip for synchronization.