AS3 localToLocal and CoordinateTools

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.

I included the entire class here, use it as you will – Rock on \m/

[as]/*
Copyright (c) 2007 John Grden

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the “Software”), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package com.rockonflash.utils
{
import flash.display.Sprite;
import flash.geom.Point;

public class CoordinateTools
{
public static function localToLocal(containerFrom:DisplayObject, containerTo:DisplayObject, origin:Point=null):Point
{
var point:Point = origin ? origin : new Point();
point = containerFrom.localToGlobal(point);
point = containerTo.globalToLocal(point);
return point;
}
}
}[/as]

    • ryan
    • May 17th, 2007

    nice ‘n handy.. hey that sounds like a detergent…

  1. I added an origin:Point argument to pass through incase you were converting mouse coordinates

  2. Hey John

    Is this any use to you. It’s a hacky version of your localToLocal with a method for 3D added (local3DToLocal3D) – I needed one myself. I took out the origin Point because it wasn’t relevant to my needs – but I hope it will be of some use to you. Thank you very much for your original and very useful work on localToLocal, it got me out of the crap a few times.

    You’ve probably already done this, so sorry if it’s old news:

    /*
    Copyright (c) 2007 John Grden

    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
    documentation files (the “Software”), to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
    to permit persons to whom the Software is furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all copies or substantial portions
    of the Software.

    THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
    THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    */

    package com.rockonflash.utils{
    import flash.display.*;
    import flash.geom.*;

    public class CoordinateTools {
    public static function localToLocal(containerFrom:DisplayObject, containerTo:DisplayObject, origin:Point=null):Point {
    var point:Point = origin ? origin : new Point();
    point=containerFrom.localToGlobal(point);
    point=containerTo.globalToLocal(point);
    return point;
    }

    public static function local3DToLocal3D(containerFrom:DisplayObject, containerTo:DisplayObject):Vector3D {
    var vector3D:Vector3D = containerFrom.transform.matrix3D.decompose()[0];
    var point:Point = new Point()
    point=containerTo.local3DToGlobal(vector3D);
    vector3D=containerFrom.globalToLocal3D(point);
    return vector3D;
    }

    }
    }

  3. Thanks, this function helped solve my confused coordinates.

  1. September 3rd, 2007
  2. September 17th, 2009

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