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?