I have the following bit of code:
protected function onEnterFrame(e:Event):void
{
var diff:Number;
// position map/tree
if (x != _targetX) {
diff = _targetX - x; // get the difference
x += diff * 0.2; // tween x position
diff = diff < 0 ? -diff : diff; // get absolute value
if (diff < 0.05) {
x = _targetX;
}
}
}
I set _targetX to only one of two values in my app: 0 or -1360. When I set it to 0 the tween executes as you would expect. When I set it to -1360 a strange thing happens… the tween executes as you would expect until the very last bit. x reaches -1359.8 and diff reaches 0.20000000000004547 and it’s at this point that it just stalls out. x no longer approaches _targetX and these values just hold. They won’t budge. As a result the if (diff < 0.05) conditional never evaluates to true and the tween logic continues to execute indefinitely.
I’m guessing it has something to do with floating point precision, but I’m unsure of a solution. Any ideas?
As you say, it could be a problem to do with floating point precision. I just put your code into an empty Flash project, using only variables instead of any actual object’s positional properties, and your code worked fine – diff reached a number less than 0.05 and the code stopped executing.
I then tried with an object on the stage, and used it’s x value, and ran into exactly the problem you described. One easy solution seems to be to use an object of type Number to do the calculations, then at the end of the statement set your object’s x value equal to the variable. I’ve pasted the code I used below, where ‘box’ is the object I put on the stage:
I hope that helps.