|
-
Senior Member
localToGlobal ?
Hi,
I have a movieclip with a movieclip and am trying to find the _x and _y coordinated of it on the main stage.
No matter what i try, it is returning the local coordinates.
Am I just being stupid or is this mad ?
-
Senior Member
From within MC:
p = new Object()
p.x = this._x
p.y = this._y
this.localToGlobal(p)
trace("global x : " + p.x + " global y : " + p.y)
Outside MC:
p = new Object()
p.x = _root.myMC.myOtherMC._x
p.y = _root.myMC.myOtherMC._y
_root.myMC.myOtherMC.localToGlobal(p)
trace("global x : " + p.x + " global y : " + p.y)
I think ? Always gets me too.
-
A very senior man
i had this one recently. have to add the coords of the movie clips together to get the actual coords. it made sense at the time somehow.
-
Senior Member
Alternatively use this:
code:
MovieClip.prototype.L2G = function(p)
{
x = p.x
y = p.y
clip = this
while(clip != _root)
{
x = x* (clip._xscale/100)
y = y* (clip._yscale/100)
x += clip._x
y += clip._y
clip = _parent
}
p.x = x
p.y = y
}
is probably exactly the same as localToGlobal
-
Untitled-1.fla
siriuss, That wouldn't work if one or more of the movie clips were rotated.
-
Senior Member
doh!
Cheers strille, forgot about that. lol
-
Senior Member
Originally posted by strille
siriuss, That wouldn't work if one or more of the movie clips were rotated.
thats the problem, I have this mc structure
_root.arrow.pointer
pointer is a blank mc on the tip of the arrow mc. I need to know the pos of pointer when the arrow is rotated.
-
Untitled-1.fla
siriuss, it wouldn't surprise me if your function was faster than localToGlobal(). If that is the case it would still be good to have when you know the clips aren't rotated (which is most of the time, right?).
-
Senior Member
The code from siriuss with localToGlobal functions should work. Remember, its important where do you call localToGlobal from. _root.localToGlobal is different then _root.mc.localToGlobal.
I used localToGlobal to calculate endpoint of robot arm in the first version of robot arm I made. Parts of hand where all mcs inside each other so they were easy to rotate. But then I abandoned that approach.
Current version has all the mcs on the root and every time one of them is rotated, position and rotation of other mcs is calculated using simple math.
http://www.tonypa.pri.ee/robootik_e.html
-
Senior Member
OK I've got this :
code:
MovieClip.prototype.L2G = function(p)
{
x = p.x
y = p.y
clip = this
trace("clip : " + clip)
while(clip != _root)
{
r = clip._rotation
// transform rotation
xr = x*Math.cos(r) - y*Math.sin(r)
yr = x*Math.sin(r) + y*Math.cos(r)
// transform scale
x = xr* (clip._xscale/100)
y = yr* (clip._yscale/100)
// add to global pos
x += clip._x
y += clip._y
clip = _parent
}
p.x = x
p.y = y
}
But it doesn't look as if it's working correctly and my brain hurts. What order are you supposed to use for scale-rotation?
-
Untitled-1.fla
I've made some changes:
code: MovieClip.prototype.L2G = function(p) {
var x = p.x;
var y = p.y;
var clip = this;
trace("clip : "+clip);
while (clip != _root) {
// transform scale
var xs = x*(clip._xscale/100);
var ys = y*(clip._yscale/100);
// transform rotation
var r = clip._rotation*(Math.PI/180);
x = xs*Math.cos(r)-ys*Math.sin(r);
y = xs*Math.sin(r)+ys*Math.cos(r);
// add to global pos
x += clip._x;
y += clip._y;
clip = clip._parent;
}
p.x = x;
p.y = y;
};
You have to transform the scale first and then rotate x and y depending on that scale. And you had r in degrees when the cos and sin functions are in radians. And you had written clip = _parent, when it should be clip = clip._parent. I just tested it quickly and it seems to work. Now you just have to make it work with movie clips that are skewed.
-
Untitled-1.fla
I've just made a quick speed test and it seems localToGlobal() is about 10 times faster... Which is a bit of a surprise. Guess it's no point working on it further...
-
Senior Member
oops I frazzled my brain so much working out the rotation transformations I completely forgot about using radians.
Cheers again strille, think I'll leave skewing to a more capable soul tho.
S.
-
Senior Member
Originally posted by strille
I've just made a quick speed test and it seems localToGlobal() is about 10 times faster... Which is a bit of a surprise. Guess it's no point working on it further...
hmm strange. maybe it accesses lower level functions or flash stores positions as local and global?
nevermind, nice exercise anyway.
-
When you know are.
This code just saved my butt. Had a problem with rotation and nested movieclips. I made a couple modifications cause I wasn't able to figure out how to use the prototype as it was.
PHP Code:
MovieClip.prototype.L2G = function() {
var temp = new Object();
temp.newX = 0;
temp.newY = 0;
var clip = this;
trace("clip : "+clip);
while (clip != _root) {
// transform scale
var xs = temp.newX*(clip._xscale/100);
var ys = temp.newY*(clip._yscale/100);
// transform rotation
var r = clip._rotation*(Math.PI/180);
temp.newX = xs*Math.cos(r)-ys*Math.sin(r);
temp.newY = xs*Math.sin(r)+ys*Math.cos(r);
// add to global pos
temp.newX += clip._x;
temp.newY += clip._y;
clip = clip._parent;
}
//trace("x : "+temp.newX+" , y : "+temp.newY);
return temp;
};
Used it like "targetMovieClip.L2G().newX" to get the X position of a nested movieclip that was in a rotated object.
Thanks to siriuss and strille.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|