Search code examples
actionscript-3flashapache-flexflex3

Width does not take into account 'x'?


I have the following code :

trace('width1 : '+ width);
_selected = new ScaleBitmap(new SearchDropDownItemSelectedBitmap(0, 0));
_selected.scale9Grid = new Rectangle(5, 5, _selected.width - 10, _selected.height - 10);
_selected.width = 109;
_selected.x = 2;
addChild(_selected);
trace('width2 : '+ width);

It displays :

[trace] width1 : 0
[trace] width2 : 109

It means that my element has the same size as its child, while the child is positionned at x=2. Why it acts like that? How could I know the real size? (Here it should be 109+2=111)


Solution

  • Flash handles the width of a display object based on it's contents. It creates the smallest possible bounding box (as seen in blue below) around the child objects. The width of the parent movie clip is the width of this bounding box.

    The centre point is not used in these calculations.

    If you need to you can simply add the MovieClips X position to it's width (_selected.x + _selected.width), but this does not take into account child content that is to the left of the centre point (negative values).

    enter image description here