About the Height and Width property of DisplayObject

來源:互聯網
上載者:User

關於DisplayObject的width和height屬性的問題,主要兩點記住:

1.當沒有使用graphics去繪製內容時, 其width和height一定是0.

2.child DisplayObject對其寬度和高度沒有影響。

注意:這裡是DisplayObject類和其一些子類。UIComponent並不如此。

There are something to explain about the height and width of DisplayObject.

1: If there is no content in the instance
of DisplayObject(except for TextField), then the height of it will be 0 even
you set it to some non-zero number”:

       private function testHeight1():void

       {

           var shape:Shape = new Shape();

           shape.height = 10;

           trace(shape.height);

       }

Result:0 

From the code above, we see that the content is null, so we can see that when we get the height
from this property:height, we get 0. But we should understand what is content?
 Some code showing below:

       private function testHeight2():void

       {

           var shape:Shape = new Shape();

           // draw a rectangle, topleft corner is (0,0),width and height is 100

           shape.graphics.drawRect(0,0,100,100);

           shape.graphics.drawCircle(100,100,
50);

           shape.graphics.drawCircle(0,0,50);

           trace("with content DisplayObject height:"+shape.height);

           trace("with content DisplayObject width:" +shape.width);

}

Result: with
content DisplayObject height:200

with content DisplayObject width:200

We see that after we use the graphics of the
DisplayObject instance to draw something, the width and height of them is calculated by what the graphics has drawn. What about
the child? What is the impact after we add child to the DisplayObject?

2. About the CONTENT
of the DisplayObject

       private function testHeight3():void

       {

           var childShape:Shape = new Shape();

           childShape.graphics.drawCircle(0,0,200);

           var sprite:Sprite = new Sprite();

           sprite.height = 20;

           sprite.width = 20;

           sprite.addChild(childShape);

           trace("After add a child, the height is:" + sprite.height);

           trace("After add a child, the width is:"  + sprite.width);

       }

Result: After add
a child, the height is:0

After add a child, the width is:0

We see that the child DisplayObject has no impact on the
width and height of the DisplayObject. This means that the height will not calculate
the children nodes of it.

So here we have an conclusion: the height and width of an DisplayObject
is not what it have drawn by itself, in another word: the content of
DisplayObject does not contain the children of it.

3. About the
relation between scaleX and width or scaleY and height

     In fact, the
width and height is calculated with the scaleX, scaleY and the real position of
it. We should remember that, after we adjust the width, in fact we have adjust the
scaleX, the scaleX and scaleY is really stored.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.