假設我有一組具有相同y位置的5個動畫片段,但按升序分佈x個位置(例如obj1.x = 0
,obj5.x = 10
),是否有AS3方法可幫助我分配它們的寬度像下修改>對齊閃光燈的選項,所以它們的x位置0和10之間等距間隔?在AS3中分配一組對象的寬度
感謝
假設我有一組具有相同y位置的5個動畫片段,但按升序分佈x個位置(例如obj1.x = 0
,obj5.x = 10
),是否有AS3方法可幫助我分配它們的寬度像下修改>對齊閃光燈的選項,所以它們的x位置0和10之間等距間隔?在AS3中分配一組對象的寬度
感謝
這是一個功能我多年前寫的,你可能會發現有用的。它假設你想要放置的東西是父母/容器的唯一孩子。您將該父項作爲第一個參數(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;
}
這個碼擴展項目horizontaly會在不同的寬度 - 第一個項目的左側邊緣在啓動,最後一項的右邊緣位於認爲「寬度」結束。
最好的問候
使其垂直傳播是微不足道的:)只是改變寬度的高度X到Y,其中appriopriate :) –
如果只想比你需要做的是通過量除以你的寬度傳播equaly原點exluding項目大小要傳播的項目,然後在循環中將此計算的步驟添加到每個項目。 –
所以你想要他們所有的寬度是相同的?或者您希望它們都具有相同數量的每個項目之間的空間/間隙,但是具有它們的原始寬度? – BadFeelingAboutThis
我的回答假設是後者,但我不知道這是你真正想要的東西。當然 – BadFeelingAboutThis