I’ve tried casting delta as a Number just to make sure but that doesn’t work… what’s interesting is that I can run:
trace(testNumber+delta);
and that will return the correct result, I just can’t store the result in a variable if any math has been done with the delta variable. I’ve only just gotten back into AS3 so I might be missing some completely fundamental here but I’m clueless at this point so any help is greatly appreciated.
Yeah, I’ve tried having it defined as well, no difference. Either way it gets assigned a value before it’s used. Like I was saying the interesting thing is that I can trace something that uses it like:
trace(3+delta);
and that works fine, however if I try to use it with the += operator it just goes to NaN.
I believe testNumber is Nan, rather than delta. check to see that you defined testNumber before the enterframe is called. I did test and this does work properly in frame 1 on a flash movie
var timePrevious:Number = 0;
var timeCurrent:Number = 0;
var delta:Number=0;
var testNumber:Number = 0;
this.addEventListener(Event.ENTER_FRAME, onGameTick);
function onGameTick(e:Event):void
{
timePrevious = timeCurrent;
timeCurrent = getTimer();
delta = (timeCurrent - timePrevious) * 0.001;
testNumber += delta;
trace(testNumber);
}
however if I start with var testNumber:Number; then I get Nan, as Nan+=delta= Nan.
you are correct that delta doesnt matter because its set in that function
edit: does anyone know why my line break spacing is not kept in the post?
Yes TheCanadian, that trace works, however if I do:
testNumber+=delta;
trace(testNumber);
I get NaN.
tbo, I’m defining testNumber as 0 so it’s definitely not NaN.
I tried what you said in a flash movie and it works for me too, but when I put the code in my class it doesn’t work -___-… What’s interesting is that if I do this in my function:
var timePrevious:Number = 0;
private var timeCurrent:Number = 0;
private var delta:Number=0;
it works fine, however if I declare the variable in the class rather than the function I get NaN.
EDIT:
I created a new project with the code in the main class and it works, so it’s something about my current class in my current project, even though the classes are basically identical.
So I found that timeCurrent was NaN, I just rewrote the class, making sure I did everything correctly and it’s working.
Still find it bizarre that I could do arithmetic in a trace statement and it would produce the correct results however if trying to assign that to a variable it didn’t like it. Oh well, it’s fixed now!
I think the [ code][ /code] tags don’t work properly. It seems to work if you use three grave accents (`) before and after your code (if that makes any sense).