1
votes

I have a fl.controls.Button that I set to size (w:300, h:200). I then add this button to an empty 'container' sprite. From what I understand, the children of a display objects are taken into account when looking at the display object's width/height parameters. But this does not seem to be entirely true for the button object?

For example:

        var container:Sprite = new Sprite();
        trace(container.width, container.height); // 0 0
        var btn:Button = new Button();
        btn.setSize(300, 200);
        this.addChild(container);
        container.addChild(btn);

        trace(btn.width, btn.height);  // 300 200
        trace(container.width, container.height); // 100 100  - Why??

        var rectangle:Sprite = new Sprite();
        rectangle.graphics.drawRect(0, 0, 500, 400);
        container.addChild(rectangle);
        trace(container.width, container.height); // 500 400

Why is the container not given the same width/height values as the button, as it is the only thing inside the sprite. Similarly, if instead I write setSize(40,40), the container still goes to size 100x100. It's making it hard for me to determine the vertical size of a container with many buttons inside of it.

2
Please explain downvote? - user1063998
If you don't mind can you also trace the container.scaleX and container.scaleY where you have it trace the width and height? - shaunhusain
scaleX and scaleY were always 1 - user1063998
aaah. but the btn seems to be scaled! which would explain the behaviour - user1063998
hmm interesting, yeah I was just shooting in the dark based on the docs saying things about not adjusting the width/height of an empty sprite because it adjusts the scale values... it is strange anyhow - shaunhusain

2 Answers

0
votes

I have experienced a similar issue with a Sprite containing a set of Shapes, I have resolved it by adding that Sprite to stage, and only then querying its size. You can just add the sprite to stage, query its size and instantly remove it from stage, so it won't get displayed, but its size will be valid.

0
votes

The problem is in your Button component. There is some "magic" with fl.control components' sizes and scales. Avoid of using them whenever is possible. Try flash.display.SimpleButton instead of fl.controls.Button.

In this exact situation, you are trying to scale zero-sized element (because width and heigth of fl.controls.Button depends ONLY on it's back's size. So, creating a new object of fl.controls.Button gives you an element with 0 width and height). Then you set it's w/h to 300/200, but in fact you do nothing, you set button's back's size only (there is no back, so you scale 0 by Infinity and still get 0). As you know, container's size depends on it's children. And you have only 1 child with 0/0 size in it (You just overwrote button's w/h visible properties, but didn't really set w/h to your values). That way, your container must be 0/0, but it is 100/100, right? It's so because of fl.controls' REAL minimum size. Every fl.controls element IN FACT is not less then 100/100, even if you get 0/0 w/h on trace.

So, I can advice you avoid of using fl.controls whenever is possible, it'll save you lots of time =)

Here is some code:

var btn:Button = new Button();
trace(btn.width, btn.height, btn.scaleX, btn.scaleY); //0 0 NaN NaN
btn.width = 300;
btn.height = 200;

trace(btn.width, btn.height, btn.scaleX, btn.scaleY);  //300 200 Infinity Infinity