If I have a rectangle on the stage, how do I find its top left tip (x,y) and the bottom right tip (x,y) in relation to the stage? It strange how I can't find this on google!
localToGlobal(point)
of DisplayObject
Converts the point object from the display object's (local) coordinates to the Stage (global) coordinates.
// assuming (0, 0) is top left
var topLeftStage:Point = myDisplayObject.localToGlobal(new Point(0, 0));
// bottom right
var bottomRightStage:Point = myDisplayObject.localToGlobal(new Point(width, height));
You can do this in one line, e.g. if the container you've added it to is a DisplayObject as well, you can write:
var rect:Rectangle = yourDisplayObject.getBounds(stage);
That will jump directly to getting you a rectangle relative to the stage. You can then access the values you mentioned specifically:
rect.bottomRight
rect.topLeft
If your object is in one container then you can just subtract the containers's position from the objects's position.
var rawx:Number = x - parent.x;
var rawy:Number = y - parent.y;
Else use localToGlobal()
like above.
Finding the top left and bottom right points of an object is easy - but you need to know where the registration point of the symbol is.
If the registration point where in the centre of the symbol:
var left:Number = x - (width / 2);
var right:Number = x + (width / 2);
var top:Number = y - (height / 2);
var bottom:Number = y + (height / 2);
If it were at the top left:
var left:Number = x;
var right:Number = x + width;
var top:Number = y;
var bottom:Number = y + height;
Etc.
if the displayobject does not start at 0,0 of the movieclip, you'll need this:
var skin:DisplayObject = ... //the MC you need to get positions of
var point : Point = skin.localToGlobal(new Point(skin.getBounds(skin).x,skin.getBounds(skin).y));
var point2 : Point = skin.localToGlobal(new Point(skin.getBounds(skin).x+skin.getBounds(skin).width,skin.getBounds(skin).y+skin.getBounds(skin).height));
and the results will be:
x=point.x;
y=point.y;
width=point2.x-point.x;
heigth=point2.y-point.y;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With