假设我有一组5个具有相同y位置的MovieClip,但是按升序排列x个位置(例如obj1.x = 0
,obj5.x = 10
),是否有一个AS3方法可以帮助我分配它们的宽度修改>下的选项在闪光灯中对齐,因此它们的x位置在0到10之间等间隔?
由于
答案 0 :(得分: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);
}
}
}
答案 1 :(得分: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;
}
此代码在给定宽度范围内展开项目 - 第一项的左边缘在开始,最后一项的右边缘在“宽度”的末尾。
最好的问候