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 ?
Printable View
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 ?
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.
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.
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
siriuss, That wouldn't work if one or more of the movie clips were rotated.
doh!
Cheers strille, forgot about that. lol
thats the problem, I have this mc structureQuote:
Originally posted by strille
siriuss, That wouldn't work if one or more of the movie clips were rotated.
_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.
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?).
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
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?
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. :p
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...
oops I frazzled my brain so much working out the rotation transformations I completely forgot about using radians. :D
Cheers again strille, think I'll leave skewing to a more capable soul tho.
S.
hmm strange. maybe it accesses lower level functions or flash stores positions as local and global?Quote:
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...
nevermind, nice exercise anyway.
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.
Used it like "targetMovieClip.L2G().newX" to get the X position of a nested movieclip that was in a rotated object.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;
};
Thanks to siriuss and strille.