Did you notice that the new version of Actionscript has a new method: the getBounds"
This method is implemented in the DisplayObject Class and so, it can be used from the MovieClip and Sprite Class (as they inherit from the DisplayObject).
The method getBounds requires a parameter named targetCoordinateSpace that indicates which DisplayObject to use and defines the coordinates to be used (local or global).
It returns an instance of Rectangle with the properties x, y, width, height, left etc, etc" of the object associated to this method.
Often, it is required to position some objects in relation to the position of some other objects and it is then necessary to do some mathematical calculi.
In this case, the method getBounds will simplify our task to do so.
Let us see how to use it"
I create a FLA and I save it as "main.fla". Into which, I create and I instantiate on stage 2 MovieClip. The first one of rectangular shape named "rect_mc" and the second one, of square shape named "square_mc".
If I wanted to position the x and y of square_mc immediately after rect_mc, I would write:
Code:
square_mc.x=rect_mc.x+rect_mc.width;
square_mc.y=rect_mc.y+rect_mc.height;
using getBounds, I could do it the following way:
Code:
var bound:Rectangle=rect_mc.getBounds(this);
square_mc.x=bound.right;
square_mc.y=bound.bottom;
and obtain the following result:
This method is really interesting when we have an application into which we need to calculate the distances of each objects in relation to the positions of some other objects. Using this method, we could simply create an instance of Rectangle with getBounds for each objects on stage and then use theirs properties left, right, top and bottom to position them.
Stay tuned !