项目中需要确定 feathers组件的宽高,但是怎么读取都是0
翻阅 api:
width | property |
width:Number
[override]
The width of the component, in pixels. This could be a value that was set explicitly, or the component will automatically resize if no explicit width value is provided. Each component has a different automatic sizing behavior, but it's usually based on the component's skin or content, including text or subcomponents.
Note: Values of the width
and height
properties may not be accurate until after validation. If you are seeing width
or height
values of 0
, but you can see something on the screen and know that the value should be larger, it may be because you asked for the dimensions before the component had validated. Callvalidate()
to tell the component to immediately redraw and calculate an accurate values for the dimensions.
Implementation
public function get width():Number
public function set width(value:Number):void
See also
所以 ,我们在把 组件添加到 显示列表后,立即调用validate()方法,把组件进行验证,则可确定宽高
如下:
confirmBtn = new feathers.controls.Button();
confirmBtn.label = "Game Over!";
addChild(confirmBtn); //为什么feathers 组件width == 0 |||
confirmBtn.validate(); //添加到显示列表后,立即验证,则可得到组件的实际宽高,否则有可能读不到
confirmBtn.x = this.width * 0.5 - confirmBtn.width * 0.5;;
confirmBtn.y = this.height * 0.5 - confirmBtn.height * 0.5;
这样我们就可以设定了,注意如果用到viewPort ,则居中显示尤为重要!