MovieClip._x and MovieClip._y

_x is a getter/setter property. When we say _x=200; the setter function performs complex procedures and the movieClip will be redrawn to that new position. It is not just assigning a new value to a variable.

The TWIP

_x=200;
_x+=3.14159;
trace(_x);

The output is 203.1 not 203.14159;

It seems that the value is trimed to 2 digit decimal. Similar "trimed" value appears in _width, _xmouse or the result of localToGlobal function. Here is the reason:

The basic unit of canvus in Flash is TWIP. A twip is 1/20 pixel that is 0.5 pixel. So, the we get a result with a precision of 0.5 pixel. We may get a _x value of 203.10, 203.15; 203.20; but will not get a value as 203.14 not to mention 203.14159;

When we assign _x=203.14159, we are trying to assign _x=4062.8318 twip; Flash take the integer part and discard the decimal part, that is 4062 twip. The result is _x=203.1;

Pleae note that, it is "triming" not "rounding"; Otherwise, we would expect the result to be 203.15 instead of 203.1;

There is a side effect to do "triming" instead of "rounding";

1 twip + 0.8 twip=1.8 twip. After triming it is 1 twip; It appears that we did not add anything to it.

1 twip - 0.8 twip=0.2 twip. After triming it is 0 twop; It appears that we make it decrese by 1 twip;

That effect is just opposite when the original value is negative value:
-1 twip+0.8 twip=0.2 twip. After triming, it is 0 twip. It appears that we added 1 twip;
-1 twip-0.8 twiip=-1.8 twiip. After triming, it is -1 twip. It appears that we did not make it decrase.

It will not happen if we add it an integer of twip.

Lets see our example:

_x=200;
_x+=3.14159;
trace(_x);//output 203.1
_x-=3.14159;
trace(_x);//output 199.95

The deviation of 0.5 pixel

Well, we add 3.14159 and then substract it by 3.14159 , the result is different from the original one. There is a difference of 0.5 pixel (one twip).

The twip value of 3.14159 pixel is 62.8318; After adding , the twip value of _x is 4062.8318; We trim to get the integer part, that is 4062; Then we do the subtraction, the result is 3999.1682; After triming it is 3999; It is convert to 199.95;

It will not happen if we add it an integer of twip.

However, it is very often that we give our clip a moving speed of 20/3 or 10*Math.cos(angle); Then we get some annoying deviation.

A hero that should stand still, may deviate 0.5 pixels each frame if we do _x+dx; _x-dx;

A car that is expected to run to-and-fro along an oblique line, may deviate off the line after several frames.

The coding technique

If we need a precision, we should avoid the triming effect by _x; We use interanl variable to represent the "position" of the clip. And only assing the value to _x when we need repaint the clip.

x=200;
x+=3.14159;
x-=3.14159;
_x=-x;