views:

221

answers:

1

When rotating a display object (around its center) the visual corner of the element moves (the actual x and y of the "box" remains the same). For example with 45 degrees of rotation the x coordinate will have increased and the y coordinate will have decreased as the top left corner is now at the top center of the "box".

I've tried to use displayObject.getBounds(coordinateSpace).topLeft however this method is simply returning the x and y of the box and thus doesn't change after an object has been rotated.

So, how do you get the x and y of a visual corner of a rotated display object?

Update: this is what I mean with the position of a visual corner after rotation --> alt text

+1  A: 

You simply need to translate the point to its parent's coordinate space.

var box:Shape = new Shape();
box.graphics.beginFill(0xff0099);
box.graphics.drawRect(-50, -50, 100, 100);  // ... the center of the rectangle being at the middle of the Shape
addChild(box);

box.x = 100; // note: should be 100 + box.width * .5 in case you want to use the topleft corner to position
box.y = 100;                    
box.rotation = 45;

// traces the result (Point)                
trace( box.parent.globalToLocal(box.localToGlobal(box.getBounds(box).topLeft)) );
Theo.T
Tried that, it simply returns the x and y of the box corner, not of the visual corner. Please see my update question for an example graphic of what I mean with "visual corner".
Tom
Hm, I just tried it with your example above and (x=0, y=-141.4) which is the correct result.
Theo.T
So getBounds.topLeft should return the corner position that I am talking about (aka the "visual" one)?
Tom
Also, what did it return without rotation (rotation = 0) ?
Tom
With a rotation of zero I get (x=-100, y=-100) as expected. My box has its center at 0,0- its position on the stage is 0,0 - it's width is 200.
Theo.T
I'd appreciate if you could share your snippet. I must be doing something wrong but can't figure it out just yet. Thanks a lot for your help so far.
Tom
Update: I worked it out. The problem was that I wasn't taking both my position and rotation wrapper parents into consideration. Thanks a lot Theo.
Tom
Ran into another problem, there's no topCenter nor topRight attribute. I wonder how to get these positions when an object has been rotated. For example, the top right corner of my example above. I guess I should make a new topic about this.
Tom