0

假设我有一组 5 个具有相同 y 位置但按升序分布 x 位置(例如obj1.x = 0, obj5.x = 10)的 MovieClip,是否有一种 AS3 方法可以帮助我分配它们的宽度,例如在 Flash 中修改>对齐下的选项,所以它们的 x 位置是否在 0 和 10 之间等距?

谢谢

4

2 回答 2

1

这是我多年前编写的一个函数,您可能会发现它很有用。它假定您想要分隔的东西是父/容器的唯一子级。您将该父级作为第一个参数 (displayObj) 传递给此函数。希望开头的评论能很好地向您解释每个参数的作用,如果不只是评论,我会澄清一下。

/**
* Distribte all the children of the specified display object on either x or  y axis
* @param    displayObj - the display oject whose children should be distributed
* @param    onX - should it distribute along the x axis (true) or the y axis (false)
* @param    spacing - how much space between children in pixels
* @param    center - should the children be centered on the opposite axis
* @param    startingX - default 0
* @param    startingY - default 0
* @param    fixedWidth - use a fixed width instead the automatic width of each child
* @param    foldPoint - how far along before the item should fold and be a new row/col
*/
public static function distributeAxis(displayObj:Sprite,onX:Boolean = true,spacing:Number = 5, center:Boolean = false, startingX:Number = 0,startingY:Number = 0,fixedWidth:Number = 0, foldPoint:Number = 0):void {

    //Loop Through Children
        var tObj:DisplayObject;
        var tNum    :Number = (onX) ?   startingX   : startingY;
        var tNum2   :Number = (onX) ?   startingY   : startingX;
        var max     :Number = 0;

        var centeringArray:Vector.<DisplayObject>
        if (center) {
            centeringArray = new Vector.<DisplayObject>();
        }

        for(var i:int = 0; i<displayObj.numChildren;i++)
        {
            tObj = displayObj.getChildAt(i);

            if (onX) {
                if (foldPoint > 0 && tNum + tObj.width > foldPoint) {
                    tNum = startingX;
                    tNum2 += max + spacing;

                    if(center){
                        distributeAxisCenterIt(centeringArray, max, onX);
                        centeringArray = new Vector.<DisplayObject>();
                    }

                    max = 0;
                }

                if(tObj.height > max) max = tObj.height;

                tObj.x = tNum;
                tObj.y = tNum2;

                if(fixedWidth > 0){
                    tNum += fixedWidth + spacing;
                }else{
                    tNum += tObj.width + spacing;
                }

                if(center){
                    centeringArray.push(tObj);
                }

            }else{
                if(tObj.width > max) max = tObj.width;

                if (foldPoint > 0 && tNum + tObj.height > foldPoint) {
                    tNum = startingY;
                    tNum2 += max + spacing;

                    if(center){
                        distributeAxisCenterIt(centeringArray, max, onX);
                        centeringArray = new Vector.<DisplayObject>();
                    }
                    max = 0;
                }

                tObj.y = tNum;
                tObj.x = tNum2;

                if(fixedWidth > 0){
                    tNum += fixedWidth + spacing;
                }else{
                    tNum += tObj.height + spacing;
                }

                if(center){
                    centeringArray.push(tObj);
                }
            }

        }


    if (center) {
        distributeAxisCenterIt(centeringArray, max, onX);
    }
}

private static function distributeAxisCenterIt(array:Vector.<DisplayObject>, max:Number, onX:Boolean = true):void {
                for each(var tObj:DisplayObject in array){
                    if(onX){
                        tObj.y += ((max - tObj.height) * .5);
                    }else{
                        tObj.x += ((max - tObj.width) * .5);
                    }

                }
            }
于 2012-11-07T22:52:04.300 回答
0

你需要使用这个:AS3 Commons UI。它拥有您想要的一切:) 带有布局/对齐等和组件

但是如果你想要一个代码而不是一个框架,这就是我用来在我自己的“框架”中跨给定宽度传播项目的方法:

    /**
     * Distributes all components using given value as a limit.
     * @param   p_nMaxWidth width of the area to spread items
     */
    protected function spreadComponents(p_nMaxWidth:Number):void
    {
        if (numChildren == 0) return;

        if (p_nMaxWidth < 0 || isNaN(p_nMaxWidth)) p_nMaxWidth = 600;
        var _oItem:DisplayObject;
        var dx:Number = 0;
        var i:int;
        var _nWidth:Number = calculateWidths();//get sum of all items widths
        var _nStep:Number = (p_nMaxWidth - _nWidth - getChildAt(numChildren - 1).width) / (numChildren-1);
        for (i = 0; i < numChildren; i++)
        {
            _oItem = getChildAt(i);
            //if(m_bResetChildrenPosition) _oItem.y = 0;//this I was using to reset or keep intact the items y position
            _oItem.x = Math.round(dx);
                dx += _oItem.width + _nStep;
        }
        //as a precaution if rounding would give incorrect position (which should be for last item exactly at the p_nMaxWidth - width (right edge aligned)).
        getChildAt(numChildren - 1).x = p_nMaxWidth - getChildAt(numChildren - 1).width;

        updateDimension();
    }


//
    /**
     * Utility method, returns width of all children (sum).
     * @return
     */
    protected function calculateWidths():Number
    {
        var _nWidth:Number = 0;
            for (var i:int = 0; i < numChildren-1; i++)
            {
                _nWidth += getChildAt(i).width;
            }
        return _nWidth;
    }

此代码在给定宽度上水平扩展项目 - 第一项的左边缘在开始,最后一项的右边缘在该“宽度”的末尾。

此致

于 2012-11-08T07:42:07.093 回答