Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Frame smoothing code

Ok, this is probably something very basic I don't understand about actionscript, but I can't seem to get anywhere with this.

I have some code that's supposed to smooth out the animation on an ENTER_FRAME loop:

private var m_lastTime:Number;
private var clock_speed:Number = 5;
    private function frameLoop(evt:Event):void 
    {
        var currTime:int = getTimer();
        var deltaTime:Number = ( currTime - this.m_lastTime ) * 0.001;
        this.m_lastTime = currTime;
        //trace(deltaTime.toString() + "," + (deltaTime * clock_speed).toString());

        // why you no work?
        var n:Number = clock_speed * deltaTime;
        trace(n);
        mcClockHand.rotation += .18;//  Number(deltaTime * clock_speed);

So as you can see, I'm calculating the time delta between frames, and multiplying it by a speed constant. I come up with a Number value and trace it to the output widow. It fluctuates between 0.14 and 0.19, averaging 0.18.

If I use that value to offset the rotation of a clockhand MC, it NEVER MOVES. If I use the constant 0.18 as shown above, it moves right along at roughly 1 rotation per 30 seconds.

So they're both Number type, and the trace shows me that they're roughly the same value. Why is one able to move the MC and the other isn't?

Thanks for any advice!

like image 434
LoveMeSomeCode Avatar asked Apr 16 '26 03:04

LoveMeSomeCode


2 Answers

On your first loop, the first calculation of deltaTime results in NaN because this.m_lastTime is null. From there, your code is trying to increment an Object that is not a number. A simple solution would be to set the m_lastTime variable to 0 when you declare it:

private var m_lastTime:Number = 0;

The better solution would be to check for a case when your calculation results in NaN.

var n:Number = clock_speed * deltaTime;
if (isNaN(n)) n = 0;
trace(n);
mcClockHand.rotation += n;//  Number(deltaTime * clock_speed);
like image 149
Corey Avatar answered Apr 17 '26 15:04

Corey


you have no initial value for m_lastTime when you first use it. If you initialize the var when you declare it, var m_lastTime:Number = getTimer(); it works.

like image 43
HotN Avatar answered Apr 17 '26 15:04

HotN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!