I recently ran into a quirk with the getBounds() and getRect() methods of DisplayObject, where the returned x and y coordinates would be extremely high (and incorrect) values. This occurs when the DisplayObject is empty, using the following code:
1 2 3 4 5 6 |
var parentObject:MovieClip = new MovieClip(); var childObject:MovieClip = new MovieClip(); var bounds:Rectangle; parentObject.addChild(childObject); bounds = childObject.getBounds(parentObject); |
Executing the above code gives the following result:
1 2 3 4 5 |
bounds = flash.geom.Rectangle x = 6710886.4 y = 6710886.4 width = 0 height = 0 |
What?! Where are these crazy numbers coming from? Since childObject was added as a child to parentObject without having its x or y coordinates modified, you would expect that x and y would be 0, but instead we get the unexpected result of 6710886.4! It is worth noting that if you try to get the bounds of an empty object using itself as the argument to getBounds(), x and y return 0 as expected. Now if we were to add anything that has a width and height to childObject, say a vector rectangle or a bitmap, the resulting x and y values are 0 as expected.
I am not totally sure what is going on here, but if you need to get the bounds of a child DisplayObject, you may want to add a check to make sure that childObject.numChildren is greater than 0 first.
Thanks, I thought I was becoming crazy. What a shame to have this “bug” in even the latest Flash player…