I want actual sizes!

Just great! yet another weird Flash “feature”. When I tried to get the width of a rotating MovieClip I got wildly variating values. This surprised me because the MovieClip only contained exact round shape. The circle was 25 px in diameter, but when I started to trace the width in an interval I got values ranging from 25 to 35.4px.

As it turned out, the width and height of the MovieClip only represent the bounding box of the original shape (when it still had a rotation of 0). The only way to get the actual width and height of the MovieClip was to use the getBoundsmethod. This method calculates the boundingbox according to the current shape and orientation of the MovieClip giving the trrue width and height.

To test this create a movieclip width a circle in it and place the code below on the first frame (I’m no fan of timeline-code but it’s ideal for testing purpose :D ).

1
2
3
4
this.onEnterFrame = function(){ 
	this._rotation += 15; 
	trace("boundingbox-width: "+(this.getBounds().xMax-this.getBounds().xMin)+"\t\twidth: "+this._width); 
}

The sample is as2 but the problem and solution are the same in as3.

I hope this helps some of you who were wondering why a 25px circel suddenly had a 35.4px width :| .

3 Responses to “I want actual sizes!”


  • The only problem with this is if you change the width of the object you are getting bounds on, the bounds don’t update unless you pass ‘this’ as a parameter, such as getBounds(this). However, this updates the bounds based on the rotation, and we’re back to not getting actual values.

    Can anybody figure out how to change both the _rotation and the _width of a movieclip and have getBounds report back an accurate width that doesn’t include the rotation?

    I can’t for the life of me figure it out…much help would be appreciated.

    Thanks!

  • I know this is quite old, however the simplest way I solve this is simply:

    function getFlatSize(clip:MovieClip):Object
    {
    var r = clip._rotation;
    clip._rotation = 0;
    var size = {w: clip._width, h: clip._height};
    clip._rotation = r;
    return size;
    }

  • Thanks Dave.
    Unfortunately when using a shape which is not a perfect circle (like an oval) this code doesn’t work and you would have to calculate the width and height manually :(

Leave a Reply