I have a function which is meant to check a variable called basetemp against a variable called newtemp and, if they are not the same, change basetemp until they are. It does this in increments of 0.1, and neither value ever adds or subtracts fractions smaller than that.
The code responsible for this function looks like this:
this.onEnterFrame = function(){
if(counter < 1){
if(_root.basetemp - _root.newtemp >= 0.1){_root.basetemp -= 0.1;}
if(_root.basetemp - _root.newtemp <= -0.1){_root.basetemp += 0.1;}
counter = 10;
} else {
counter -= 1;
}
I originally tried a more simple if(_root.basetemp > _root.newtemp){} approach, but the result kept coming back as “not equal” even when the variables were identical. As a result the function would change basetemp and end up going back and forth between the correct number and a value -0.1 lower or higher than the correct number.
The method I’ve pasted above is more reliable, but still has a problem of its own: when counting down from a higher number it works perfectly, but when counting upward from a lower number it always stops at a value -0.1 lower than what it should be (for instance, if it should reach 3.0 it will stop at 2.9).
Switching the order of the two conditions so that the one that adds comes before the one that subtracts does not change the results. Changing the value of the difference in the condition to a lower or higher value (like 0.2 or 0.5) also doesn’t help–changing it to 0.2 makes it stop at -0.2 away instead of -0.1 away, but 0.5 does not appear to change the results at all.
So why is this happening? Flash is stubbornly refusing to perform these particular mathematical problems correctly, and I’m out of ideas on how to make it behave.