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 ;)

Posted in Flash, Flex. 9 Comments »

9 Responses to “GlobalToLocal / LocalToGlobal? Please. Meet localToLocal and be done with it.”

  1. Joan Garnet Says:

    So simple… so nice…
    Thanx!

  2. GlobalToLocal / LocalToGlobal? Meet - localToLocal! at bling bling nivas.hr blog - white and nerdy edition Says:

    [...] John Grden came up with great idea to deal with coordinate conversion in Flash – localToLocal! He first implemented it way back in Flash6. [...]

  3. Scottae Says:

    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.

  4. John Grden Says:

    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”

  5. matt Says:

    Any luck with the AS3 version?

  6. RockOnFlash m/ :: John Grden » AS3 localToLocal and CoordinateTools Says:

    [...] I had said I’d post the AS3 version of localToLocal and here it is! I’ll be adding some modifications for Flex2 stuff as I get more familiar with it, but this works as it did with AS2. I’ve tested with Flex2 and basic AS3 apps and works very well. [...]

  7. Ralph Says:

    Awesome so glad I found this! Thank you John!

  8. JS Says:

    Here’s how to do it in AS3:

    function localToLocal(fr:MovieClip, to:MovieClip):Point {
    return to.globalToLocal(fr.localToGlobal(new Point()));
    };

  9. xero / the.fontvir.us Says:

    thanx js, that was exactly what i was looking for ;D


Leave a Reply