Why does obj.getBounds().height give a larger height than obj.height?
Posted
by TC
on Stack Overflow
See other posts from Stack Overflow
or by TC
Published on 2010-03-03T13:49:00Z
Indexed on
2010/05/19
18:20 UTC
Read the original article
Hit count: 151
I'm new to Flash and ActionScript, but managing quite nicely. One thing that is continuously getting in my way are the width
and height
properties of DisplayObject(Container)
s. I'm finally starting to get my head around them and learned that the width and height of a Sprite
are determined solely by their contents for example.
I do not understand the following though: I've got a Sprite
that I add a bunch of Button
s to. The buttons all have a height
of 30 and an y
of 0. As such, I'd expect the height
of the containing Sprite
to be 30. Surprisingly, the height
is 100.
The Adobe documentation of the height
property of a DisplayObject
states:
Indicates the height of the display object, in pixels. The height is calculated based on the bounds of the content of the display object.
Apparently, the 'bounds' of the object are important. So I went ahead and wrote this little test in the Sprite
that contains the Button
s:
for (var i:int = 0; i < numChildren; ++i)
{
trace("Y: " + getChildAt(i).y + " H: " + getChildAt(i).height);
trace("BOUNDS H: " + getChildAt(i).getBounds(this).height);
}
trace("SCALEY: " + scaleY + " TOTAL HEIGHT: " + height);
This code iterates through all the objects that are added to its display list and shows their y
, height
and getBounds().height
values. Surprisingly, the output is:
Y: 0 H: 30
BOUNDS H: 100
... (5x)
SCALEY: 1 TOTAL HEIGHT: 100
This shows that the bounds of the buttons are actually larger than their height (and the height that they appear to be, visually). I have no clue why this is the case however. So my questions are:
- Why are the bounds of my buttons larger than their height?
- How can I
set the bounds of my buttons so that
my
Sprite
isn't larger than I'd expect it to be based on the position and size of the objects it contains?
By the way, the buttons are created as follows:
var control:Button = new Button();
control.setSize(90, 30);
addChild(control);
© Stack Overflow or respective owner