Friday, July 16, 2010

[AS2] Understanding localToGlobal or globalToLocal

I don't feel that the Flash Help docs explain localToGlobal/globalToLocal very well -- I never fully understood it. I might have simply assumed that localToGlobal would always give me a point Obejct with values referring to the Stage ("_root") scope.

However, localToGlobal returns a modified point (x,y) Object related to the parent ("_parent") of the scope movieClip that calls the localToGlobal function. It does not return a point Object related to the Stage ("_root"), unless Stage is the parent of the movieClip triggering localToGlobal.


Example 1)

var point:Object = {x:truck.truckWheel._x, y:truck.truckWheel._y};

truck.truckWheel.localToGlobal(point);

// This modifies point Object values to reflect the coordinates within truck timeline, the "_parent" of truck.truckWheel.

// You now have values that show you coordinates of truckWheel (point.x and point.y) within the truck movieClip, and for most cases this is probably useless to you.


Example 2)

var point:Object = {x:truck.truckWheel._x, y:truck.truckWheel._y};

truck.localToGlobal(point);

// This modifies point Object values to reflect the coordinates within Stage, the "_parent" of truck.

// You now have values that show you coordinates of truckWheel (point.x and point.y) on the Stage, which is probably more useful in most situations where you would want to use this function.