PDA

View Full Version : How to know whether an object is centered or not


Darko74
09-24-2009, 01:17 PM
I'd like to find out how to know whether an object is centered (its 0,0 coords are on the center point as happens by default to objects which are created on edit time) or left aligned (its 0,0 coords are located on its top left corner, as happens by default to objects which are created dynamically), so that I can apply different actions to it in order to zoom it in/out always from its center point.

Darko74
09-24-2009, 10:49 PM
Someone who gives me a hand please! :rolleyes:

ASWC
09-24-2009, 11:42 PM
use getRect() method to find out.

Darko74
09-25-2009, 09:04 AM
Thank you ASWC! I'll try it! :)

-Ev-
09-25-2009, 03:09 PM
I would actually use the getBounds() method, which is visually more accurate than getRect() because it includes the pixels of any strokes you have on graphics.

myObject.getBounds(myObject);
will give you a rectangle that defines that object's contents relative to its registration point (0, 0).

So this might be what you need:
var isCentered:Boolean = (myObject.getBounds(myObject).x == myObject.width * -.5);
In english: "is the x-value of this object's contents equal to half its width, but negative?" You can optionally add a check for the y value as well. Depends on how specific you need to check.

Darko74
09-25-2009, 09:56 PM
Hey! I finally used getRect() but I like what you set out, mainly the condition. I didn't think about the condition the same way and merely did something like this:

if (myObject.getRect(myObject).x < 0 && myObject.getRect(myObject).y < 0) { ... }

Although my condition is ok for what I'm doing, yours is more precise. So I'll use it and will also use getBounds()

Thanks a lot for your help!