开发者

How to manage positions of DisplayObjects in actionscript?

开发者 https://www.devze.com 2023-04-02 02:22 出处:网络
I use the following code to set position of a DisplayObject to center. obj.x = (screen_width - obj.width) / 2;

I use the following code to set position of a DisplayObject to center.

obj.x = (screen_width - obj.width) / 2;

or

obj.x = (parentObj.width - obj.width) / 2;

And I use the following code to show 2 objects vertically.

obj2.x = obj1.width + interval

And I use the following code to show 2 objects vertically and set the position of them to center.

var obj3:Sprite = new Sprite;

obj3.addChild(obj1);
obj3.addChild(obj2);

obj2.x = obj1.width + interval;
obj3.x (screen_width - obj3.width) / 2;

Ar开发者_如何学Goe the above codes a good way to manage positions of DisplayObjects?

Are there better and simple ways to do that?


Well you can also add them into a HGroup or a VGroup, (or HBox and VBox if you are using Flex 3). But when I want to implement a vertical layout without using these kind of elaborate objects, I usually create a "update()" method (note that the following code may contain syntax errors : it is just provided as an algorithm example):

private function update():void
{
    // The vertical gap between elements
    var gap:int = 4;

    // The cumuled height
    var cumuledHeight:Number = 0;

    // The number of elements
    var n:int = numElements;

    // The element at each loop
    var o:IVisualElement;
    for (var i:int = 0; i<n; i++) {

        // Gets the element
        o = getElementAt(i);

        // Sets its vertical position
        o.y = cumuledHeight + gap*i;

        // Updates the cumuled height
        cumuledHeight += o.height;
    }
}


Yes, there is a simpler way by using Flex to center it with horizontalCenter=0 and verticalCenter=0.

0

精彩评论

暂无评论...
验证码 换一张
取 消