Virtual/fixed EnterFrame

Hi,
I have an animation based on the deltatime of each frame, that is updated at each EnterFrame event, but this suffers of a big problem: if the game hangs for too long (I can reproduce this “pause” simply holding the title bar in the Flash target or running HTML5 target on a stuttering smartphone) I can miss too many frames and consequently too many EnterFrame events.

Is it possible to use “virtual” EnterFrame events that triggers even when the real visible frame is skipped? I.e. physical engines distinguish between these two kind of frames, fixed and visible, but I don’t think OpenFl/AS allow for this. Does bare Haxe allow for this?
Thanks

You can update physic in different function with virtualy-fixed frame rate, something like that:

static inline var UPDATE_INTERVAL : Float = 1.0 / 60.0; // 60 times per second
static inline var MAX_UPDATES_PER_TICK : Int = 10; // limit updates per tick for slow computers
var lastTime : Float = -1.0;

function update() : Void {
    // called with fixed rate
}

function render() : Void {
    // called on every ENTER_FRAME
}

function onEnterFrame() : Void {
    var elapsedTime = Timer.stamp();

    if (lastTime < 0.0) {
        lastTime = elapsedTime;
        update();
    } else if (elapsedTime - lastTime > UPDATE_INTERVAL) {
        var count : Int = 0;

        while (elapsedTime - lastTime > UPDATE_INTERVAL) {
            if (count >= MAX_UPDATES_PER_TICK) {
                // too slow computer, skip updates
                lastTime = elapsedTime;
                break;
            }

            update();
            lastTime += UPDATE_INTERVAL;
            count++;
        }
    }

    render();
}

how to use:

function update() : Void {
    // move character by 1 pixel 60 times per second
    // (this is the same as move character by 60 pixels per second)
    heroSprite.x += 1.0;
}

function render() : Void {
    // do nothing in this example.
}

Or you can use delta-time approach:

static inline var MIN_UPDATE_INTERVAL : Float = 1.0 / 60.0;
var prevTime : Float = -1.0;

function update(dt : Float) {
    // called almost on every ENTER_FRAME,
    // dt is greater than minimum interval
}

function render() : Void {
    // called on every ENTER_FRAME
}

function onEnterFrame() : Void {
    var elapsedTime = Timer.stamp();

    if (elapsedTime - lastTime > MIN_UPDATE_INTERVAL) {
        update(elapsedTime - lastTime);
        lastTime = epalsedTime;
    }

    render();
}

how to use:

function update(dt : Float) : Void {
    // move character by 60 pixels per second
    // (this is almost the same as move character by 1 pixel 60 times per second)
    heroSprite.x += 60.0 * dt;
}

function render() : Void {
    // do nothing in this example.
}

First variant is simpler in many cases, but second variant will give very smooth animation.
Usually I use both two variants - first for AI, second for animation.


By the way, I just remember about one super amazing book - http://gameprogrammingpatterns.com/ (reading online available for free)
Chapter about game loops - http://gameprogrammingpatterns.com/game-loop.html