ActionScript 2 to ActionScript 3 weirdness – getBounds()

In AS2, we can use the movieclip method getBounds returns 4 values: xMin, xMax, yMin and yMax, which can be traced like so:

var bounds:Object = this.getBounds(this._parent);
    for (var i in bounds) {
    trace(i+" --> "+bounds[i]);
}

I’m getting the dimensions of the movieclip my current clip (this) is attached to. The trace reveals:

yMax --> 167.65
yMin --> 103
xMax --> 789.95
xMin --> 725.3

(I’m animating a basketball of a radius of 40, which is inside the parent clip – so its bounds change as the ball moves. In this case we can see the clip is about 64 pixels wide and high by subtracting xMin and yMin from xMax and yMax respectively).

OK, now, to do the same thing in AS3 we write:

var bounds:Object = this.getBounds(this.parent);
trace("Basketball:::handleMovement::bounds: " + bounds);

The trace reveals:

Basketball:::handleMovement::bounds: (x=673, y=84.3, w=53.10000000000002, h=53.10000000000001)

More or less the same thing – in this case, the parent is 53 x 53 pixels. Note that the value x is the same as xMin in AS2, and y is the same as yMin in AS2. So you think we could add w and h to them respectively to derive xMax and yMax, something like this:

var xMax = bounds.x + bounds.w;

Try it and you’ll get this compilation error:

ReferenceError: Error #1069: Property w not found on flash.geom.Rectangle and there is no default value.

You need to use the bounds.width property (because getBounds() now returns a Rectangle, not an Object with properties), like so:

var yMax = bounds.x + bounds.width;

I mean, that wouldn’t bother me too much except – WTF does it trace “bounds.h” and “bounds.w”? Nice to see that when Macromedia and Adobe merged, they kept on the Department of Doing Stupid Crap that Makes No Sense…

There are no comments on this post

Leave a Reply

You must be logged in to post a comment.