AS3 localToLocal and CoordinateTools

16 05 2007

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]


Actions

Information

6 responses

17 05 2007
ryan

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

3 09 2007
jax » Blog Archive » localToGlobal

[...] Persze package-ben az igazi: http://www.rockonflash.com/blog/?p=49 Azért vicces, hogy még copyright is van [...]

10 09 2007
John Grden

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

23 11 2008
York Gibson

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

}
}

17 09 2009
Flex and Flash Developer – Jesse Warden dot Kizz-ohm » Blog Archive » AIR App: Powerz

[...] …and Grden’s localToLocal function [...]

20 09 2009
Keith H

Thanks, this function helped solve my confused coordinates.

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 )

Connecting to %s




Follow

Get every new post delivered to your Inbox.