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😉

  1. So simple… so nice…
    Thanx!

    • Scottae
    • November 26th, 2006

    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.

  2. 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”

    • matt
    • May 16th, 2007

    Any luck with the AS3 version?

    • Ralph
    • November 2nd, 2007

    Awesome so glad I found this! Thank you John!

    • JS
    • September 19th, 2008

    Here’s how to do it in AS3:

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

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

  4. Very useful!
    Local to Local transformation made really easy!

    Thanks,
    Pedro

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s