Is using Entity.update() and FP.elapsed a reliable way to implement timers?


(John) #1

The officially recommended way to implement timers in a FlashPunk game is to override Entity.update() in various places and to either: 1) rely on a fixed timestep, or 2) rely on FP.elapsed.

I’m just starting to learn FlashPunk, and there may be more to the story than what I’m seeing, but I’m a little concerned about implementing timers this way. For one thing, you can’t ever really rely on a specified pace at runtime - lag spikes and such can always interfere with that.

Alright, so why not use FP.elapsed for either kind of timestep, as an extra precaution? Because it appears to round things off just a little, which can cause things to become inaccurate over time. Generally, when implementing a timer, it would probably be much better to check either the time on the computer, the number of milliseconds since the swf first started running, or something else to that effect.

Which makes me wonder about using flash.utils.getTimer(), but where would you put it? I don’t want to code this in an unclean way or inefficient way, which would rule out creating a static class with a property for this that keeps getting accessed everywhere. FlashPunk is heavily centered around Entity.update(), so I wouldn’t want to interfere with the rhyme and reason of usig the update() function.

Etc. Maybe somebody knows more about FP.elapsed and can explain that it’s actually quite reliable? Or maybe another solution would be better? I glanced at one post where something to do with tweening was mentioned, but I’m a little inexperienced specifically with game development, so I’m trying to take care of this now and learn about tweening a little further down the road. Thank you.


(John) #2

Well, this might have been a false alarm after all. This code was in class Engine:

        private function onEnterFrame(e:Event):void
        {
            // update timer
            _time = _gameTime = getTimer();
            FP._flashTime = _time - _flashTime;
            _updateTime = _time;
            FP.elapsed = (_time - _last) / 1000;
            if (FP.elapsed > maxElapsed) FP.elapsed = maxElapsed;
            FP.elapsed *= FP.rate;
            _last = _time;

Essentially it looks like this bridge is already being crossed in class Engine, where FP.elapsed is getting set. getTimer() is being called each time a frame is entered into, and FP.elapsed is just getting set to the difference between the current call’s return value and the previous call’s return value, in seconds. This makes me a lot more comfortable with using FP.elapsed.

My concern previously was that, if it rounded off even slightly between frames, the difference between the round-off and reality for previous frames would not be taken into account when calculating the value for subsequent frames, but this doesn’t appear to be an issue. The thread’s still open though.


(Jacob Albano) #3

Flashpunk’s timing system is really pretty amazing. Unless you need microseconds of precision (and if so, why are you using Flash?) using FP.elapsed and an Entity updating is a perfectly fine way to set up timers.

If you want to make it even easier, you can use an Alarm.

var timer:Alarm = new Alarm(duration, onComplete, Tween.ONESHOT);
function onComplete() {
    trace("finished!");
}

// add it to just about anything and it will be updated
world.addTween(timer, true);
entity.addTween(timer, true);
FP.tweener.addTween(timer, true);

(Ultima2876) #4

Yup, I wouldn’t be concerned about timing accuracy; there are rhythm action games made with FlashPunk that require very very precise timing indeed, and they worked well!


(John) #5

Thanks. Got a question though: Why are Alarms considered tweens?


(Jacob Albano) #6

More or less for convenience; they subclass Tween for the timing functionality and so they can be added to a Tweener instead of updated manually.


(Zachary Lewis) #7

A tween is just a value interpolation from 0 → 1 over a finite time t. Alarm is pretty much the most basic Tween. It doesn’t have to ease or update any values, and when it’s done it calls onComplete as a callback (like all Tweens do).


(Ultima2876) #8

Alarms are like the tween of tweens. They tween time itself, man.

[The real answer is what @zachwlewis said above]