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!
On your first loop, the first calculation of
deltaTimeresults inNaNbecausethis.m_lastTimeis null. From there, your code is trying to increment an Object that is not a number. A simple solution would be to set them_lastTimevariable to0when you declare it:The better solution would be to check for a case when your calculation results in NaN.