GlobalToLocal / LocalToGlobal? Please. Meet localToLocal and be done with it.
It’s simple: Whenever you do coordinate conversions, you do one, then you do the other. Rarely are you just trying to convert to _level0 stage or from _level0 to a clip. It’s 99.9% a movieclip to a movieclip conversion! And when you do that, you use both globalToLocal and localToGlobal.
I came up with localToLocal way back in Flash6 and realized that I still use it to this day and coordinate conversion was never so easy. Works every time and I can pass original x/y values if I want to (IE: mouse coordinates to be converted). I had this posted on an old site a while back and thought I’d put it out here for archive sake.
[as]class com.rockonflash.utils.CoordinateTools
{
public static function localToLocal(from:MovieClip, to:MovieClip, origin:Object):Object
{
var point:Object = origin == undefined ? {x: 0, y: 0} : origin;
from.localToGlobal(point);
to.globalToLocal(point);
return point;
}
}[/as]
Just use it to create your point object and you’re done:
var point:Object = CoordinateTools.localToLocal(from:MovieClip, to:MovieClip); trace(point.x + " : " + point.y)
I’ll be putting out the AS3 version as soon as i have time to test it 😉
So simple… so nice…
Thanx!
Very nice.
Something I’ve been doing since Flash 8 is using the new flash.geom.Point class instead of the generic Object class for the point object. I guess it doesn’t really matter, but when doing my class editing in Eclipse, I get warnings with the x and y properties in Object. It doesn’t screw anything up, but it’s annoying. Using the Point class which has x and y properties, it works out great. And it seems more intuitive too. It is a point.
yeah, that’s a great point, problem is flash package is Flash8 only ;(
I suppose I could return some sort of proprietary data object, but then I think that might defeat the purpose of “ease of use”
Any luck with the AS3 version?
Awesome so glad I found this! Thank you John!
Here’s how to do it in AS3:
function localToLocal(fr:MovieClip, to:MovieClip):Point {
return to.globalToLocal(fr.localToGlobal(new Point()));
};
thanx js, that was exactly what i was looking for ;D
Very useful!
Local to Local transformation made really easy!
Thanks,
Pedro