y 位置が同じで x 位置が昇順で広がっている 5 つのムービークリップのセットがあるとします (例: obj1.x = 0
) 。 [変更] > [フラッシュで整列obj5.x = 10
] の下のオプションのように幅を分散するのに役立つ AS3 メソッドがあるので、それらの x 位置はは 0 から 10 の間で等間隔ですか?
ありがとう
y 位置が同じで x 位置が昇順で広がっている 5 つのムービークリップのセットがあるとします (例: obj1.x = 0
) 。 [変更] > [フラッシュで整列obj5.x = 10
] の下のオプションのように幅を分散するのに役立つ AS3 メソッドがあるので、それらの x 位置はは 0 から 10 の間で等間隔ですか?
ありがとう
これは、私が何年も前に書いた便利な関数です。間隔をあけたいものは、親/コンテナーの唯一の子であると想定しています。その親を最初のパラメーター (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);
}
}
}
これを使用する必要があります: 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;
}
このコードは、指定された幅でアイテムを水平に広げます。最初のアイテムの左端が開始点、最後のアイテムの右端がその「幅」の最後になります。
よろしくお願いします