Recipe 7.12 Getting a Movie Clip's Boundaries

7.12.1 Problem

You want to know the minimum and maximum x and y coordinates that determine the edges of a movie clip (its bounding box).

7.12.2 Solution

Use the getBounds( ) method.

7.12.3 Discussion

You can determine the left, right, top, and bottom coordinates of a movie clip within its parent movie clip using a combination of the _x, _y, _width, and _height properties:

left   = myMovieClip._x;
right  = left + myMovieClip._width;
top    = myMovieClip._y;
bottom = top + myMovieClip._height;

While this technique works, it is rather intensive and inefficient when compared to the getBounds( ) method. The getBounds( ) method returns an object with four properties:


The leftmost x coordinate of the movie clip


The rightmost x coordinate of the movie clip


The topmost y coordinate of the movie clip


The bottommost y coordinate of the movie clip

The four properties define the bounding box of the visible content within the movie clip. (Note that y coordinates increase as you move down the screen, which is the opposite of the Cartesian coordinate system.) When you call getBounds( ) without parameters, it returns an object with values relative to the movie clip's coordinate system. You can also pass the method a reference to a target coordinate system (another movie clip) for which you want the returned object's values to be given:

// Include from Chapter 4 for its drawCircle(  ) method.
#include ""

// Create a new movie clip and draw a circle in it.
_root.createEmptyMovieClip("circle_mc", 1);
circle_mc.lineStyle(1, 0x000000, 100);

// Move the circle to (200,200) within _root.
circle_mc._x = 200;
circle_mc._y = 200;

// Get the bounds of the circle within circle_mc as well as relative to the _root
// coordinate space.
insideBounds = circle_mc.getBounds(  );
parentBounds = circle_mc.getBounds(_root);

// Loop through the bounds objects and list the values.
trace("inside bounds:");
for (var val in insideBounds) {
  trace(val + ": " + insideBounds[val]);

trace("parent bounds:");
for (var val in parentBounds) {
  trace(val + ": " + parentBounds[val]);

/* Output window shows:
inside bounds:
yMax: 101
yMin: -101
xMax: 101
xMin: -101
parent bounds:
yMax: 301
yMin: 99
xMax: 301
xMin: 99

7.12.4 See Also

Recipe 7.17

    Part I: Local Recipes
    Part II: Remote Recipes