« LoadVars Object :: how it works, and then some | Main | Star Wars :: Escape of the Falcon »
localToLocal :: coordinate conversions made easy
You might be interested in this function that I put together, which is REALLY simple, but I use it ALL the time for my conversions and I never have to worry about targeting the right timelines etc, like I do when using globalToLocal/localToGlobal.
First, here is the first example that you should read through to help understand localToGlobal and what it does:
localToGlobal / globalToLocal conversions:
http://www.acmewebworks.com/downloads/localToGlobal_globalToLocal_example.swf
Now for the fun stuff! This is a _global function you can use called "localToLocal" to convert any clip at any time to any timeline. And the best part? It totally makes sense when you use it. It takes the confusion out of using localToGlobal and so forth.
_global.localToLocal(MovieClip_from, MovieClip_to);
from: is the movie clip that you want to map or place.
to: is the movie clip who's coordinates you want to match
IE:
you have 2 movie clips:
mc_a.mc_aa.mc_aaa
mc_b.mc_ba.mc_baa -
if you want to match mc_aaa's coordinates to mc_baa's coordinates (which lives in mc_ba's timeline), then you send mc_aaa as the "from", and mc_ba (or mc_baa._parent) as the "to"
sample:
////////////////////////////////////////////
var oP:Object = _global.localToLocal(mc_aaa, mc_ba);
trace(oP.x, oP.y);
_global.localToLocal = function(from, to)
{
var point:Object = {x: 0, y: 0};
from.localToGlobal(point);
to.globalToLocal(point);
return point;
}
/////////////////////////////////////////////
Works every time.
So, if you say to your self "where does this bullet_mc (Which is 3 levels down from the main timeline) hit in the _level0 timeline/stage where my badGuy_mc is located?"
The code would be:
_global.localToLocal(this.bullet_mc, _level0);
See the important thing to note is that your converting to TIMELINES/containers of movie clips - like the example above, I want to compare my bullet to another movie IN the root timline. So, instead of putting the other movie's instance/path, I put in it's parent's path as to say "Use THOSE coordinates out on root, where my other movie is."
To see it in simple action:
http://www.acmewebworks.com/FLAS/hereToThere_test.swf
FLA:
http://www.acmewebworks.com/FLAS/hereToThere_test.fla
Any questions? Email me at [email protected]
December 21, 2004 in Flash ActionScript | Permalink
TrackBack
TrackBack URL for this entry:
http://www.typepad.com/t/trackback/1569033
Listed below are links to weblogs that reference localToLocal :: coordinate conversions made easy:
Comments
Hi John,
Thanks for sharing this little tidbit. It ended up helping me on a little menu component that I'm working on right now. I converted the function to a utility class that I found fit a little better with my project rather than using _global. Below is the code if you or anyone else is interested.
/**
* A Utility class for determining the correct postions of MovieClips.
* Thanks to John Grden [email protected] for the original localToLocal function.
*
* @author Chris Allen
*/
class com.adaptivedesigners.utils.PositionConverter{
/**
* Returns a point object to determine the _x and _y positions of
* a specified MovieClip relative to another.
*
* @param from MovieClip the movie clip that you want to map or place.
* @param to MovieClip the movie clip who's coordinates you want to match
*/
public static function localToLocal(from:MovieClip, to:MovieClip):Object {
var point:Object = {x: 0, y: 0};
from.localToGlobal(point);
to.globalToLocal(point);
return point;
}
}
Posted by: Chris Allen | May 1, 2005 02:24 PM
Hey Chris! That's outstanding, thanks for doing that and sharing, much appreciated!
Posted by: John Grden | May 1, 2005 03:46 PM
thanks! very helpful
Posted by: matt | June 22, 2005 10:00 AM
Glad it helped out Matt!
Posted by: John Grden | June 22, 2005 10:33 AM