2011-08-25 42 views

回答

2

如果我們可以假設這些影片剪輯被添加到它們自己的容器中(例如,只包含這些移動剪輯的容器),並且它們是按順序添加的,那麼這很容易。

container.removeChild(someMC); 
for(var i:int = 0; i < container.numChildren; i++) { 
    if(i == 0) { 
     //this is the first, so position at 0 
     container.getChildAt(i).y = 0; 
    } else { 
     //position based on previous child 
     var prev:DisplayObject = container.getChildAt(i-1); 
     container.getChildAt(i).y = prev.y + prev.height; 
    } 
} 
+0

非常感謝。 – Kerberos

+0

這裏的解釋也適用於艾米的解決方案,但她很整潔。 – shanethehat

6

試着這麼做

var top:int = 0; 
for (var i:int=0;i<numChildren;i++) { 
    var child:Sprite=getChildAt(i); 
    child.y = top; 
    top += child.height; 
} 

編輯的解釋

比方說,您要刪除的movice夾單擊時。在主文檔類,你可能有這樣的事情(假設所有實例都出現在第1幀,其他什麼都沒有):

//constructor 
public function MainDocumentClass() { 
    for (var i:int; i<numChildren; i++) { 
    var child:Sprite = getChildAt(i) as Sprite; 
    child.addEventListener(MouseEvent.Click, deleteMe); 
    } 
} 

protected function deleteMe(e:MouseEvent):void { 
    var child:Sprite = e.currentTarget as Sprite; 
    child.removeEventListener(MouseEvent.Click, deleteMe); 
    rearrange(); 
} 

protected function rearrange():void { 
    var top:int = 0; 
    for (var i:int=0;i<numChildren;i++) {  
     var child:Sprite=getChildAt(i);  
     child.y = top;  
     top += child.height; 
    } 
} 
+0

非常感謝,但我需要更多的解釋。因爲我對as3不太好。 – Kerberos

1

你並不需要重新定位的一切。從上一個索引開始。

編輯:我添加了單擊處理邏輯下艾米的職位邏輯

//constructor 
public function MainDocumentClass() 
{ 
    for (var i:int; i<numChildren; i++) 
    { 
     var child:DisplayObject = getChildAt(i) as DisplayObject ; 
     child.addEventListener(MouseEvent.Click, deleteMe); 
    } 
} 

//click handler 
protected function deleteMe(e:MouseEvent):void 
{ 
    //generic line to remove a listener 
    IEventDispatcher(event.target).removeEventListener(event.type, arguments.callee); 

    var pos:int = container.getChildIndex(e.target as DisplayObject); 

    if(pos<0) 
     return; 

    container.removeChildAt(pos); 
    rearrange(pos); 
} 

protected function rearrange(pos:int = 0):void 
{ 
    for(; pos< container.numChildren; pos++) 
    { 
     if(pos == 0) 
     { 
      //this is the first, so position at 0 
      container.getChildAt(pos).y = 0; 
     } 
     else 
     { 
      //position based on previous child 
      var prev:DisplayObject = container.getChildAt(pos-1); 
      container.getChildAt(pos).y = prev.y + prev.height; 
     } 
    } 
} 
+0

非常感謝。還有一件事,我怎樣才能在你的建議帖子中同時添加每個MC的點擊事件? – Kerberos

0
var count:Number = 0; 
var mc:Sprite; 
var i:uint; 
var pattern:RegExp = /\d+/; 
btn_mc.addEventListener(MouseEvent.CLICK, action); 
container_mc.addEventListener(MouseEvent.CLICK, action); 
function action(e:MouseEvent):void 
{ 
    var str:String = e.target.name; 
    if(str == "btn_mc") 
    { 
     mc = new Sprite(); 
     mc.graphics.beginFill(Math.random() * 0xffff00,1); 
     mc.graphics.drawRect(0,0,20,20); 
     mc.graphics.endFill(); 
     mc.buttonMode = true; 
     container_mc.addChild(mc); 
      mc.y = mc.width * count * 1.1; 
     mc.name = "item" + count; 
     count++; 
     mc.addEventListener(MouseEvent.CLICK, action0); 
    } 
} 
function action0 (e:MouseEvent):void 
{ 
    container_mc.removeChild(container_mc.getChildByName(e.target.name)); 
    var no:Number = e.target.name.match(pattern); 
    var diff:Number = count - no; 
    for(i = no+1; i< no+diff; i++) 
    { 
     container_mc.getChildByName("item"+i).y -= mc.width * 1.1; 
    } 
    count --; 
    for(i = 0; i< container_mc.numChildren - 1; i++) 
    { 
     mc.name = ""; 
     container_mc.getChildAt(i+1).name = "item"+i; 
    } 
    trace(container_mc.numChildren); 
} 

ü需要你的舞臺上創建在container_mc和btn_mc。

+0

請停止將標記問題設置爲[flash-cs5],除非從CS5是正在使用的IDE的問題清楚。 – shanethehat

+0

@shanethehat:as3不支持flash-cs5嗎? – Benny

+0

自從CS3以來,在每個Flash IDE中以及在根本不需要使用IDE的Flex和AIR應用程序中都使用ActionScript 3。我會說,除非明確OP的版本是什麼,否則只需[flash]標籤就足夠了。 – shanethehat

3

你可以創建一個自定義DisplayObjectContainer類並重寫addChild()addChildAt()removeChild()removeChildAt()方法,使他們不僅添加或刪除顯示對象,但也重新安排所有子顯示對象的位置了。我提出一個例子來說明這一點:

package 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 
    import flash.text.TextField; 

    public class Main extends Sprite 
    { 
     public function Main():void 
     { 
      if (stage) init(); 
      else addEventListener(Event.ADDED_TO_STAGE, init); 

     }// end function 

     private function init(e:Event = null):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, init); 

      var vGroup:VGroup = new VGroup(); 
      var blocks:Vector.<Sprite> = new Vector.<Sprite>(); 

      for (var i:uint = 0; i < 5; i++) 
      { 
       blocks.push(getBlock(i + 1, 100, 100)); 
       vGroup.addChild(blocks[i]); 

      }// end for 

      addChild(vGroup); 

      vGroup.removeChild(blocks[3]); 
      vGroup.removeChildAt(0); 
      vGroup.addChildAt(getBlock(6, 100, 100),1); 

     }// end function 

     private function getBlock(id:int, width:Number, height:Number):Sprite 
     { 
      var sprite:Sprite = new Sprite(); 
      sprite.graphics.lineStyle(1, 0x000000); 
      sprite.graphics.beginFill(0xFFFF00); 
      sprite.graphics.drawRect(0, 0, width, height); 
      sprite.graphics.endFill(); 

      var textField:TextField = new TextField(); 
      textField.text = id.toString(); 
      sprite.addChild(textField); 
      return sprite; 

     }// end function 

    }// end class 

}// end package 

import flash.display.DisplayObject; 
import flash.display.Sprite; 

internal class VGroup extends Sprite 
{ 
    override public function addChild(child:DisplayObject):DisplayObject 
    { 
     var displayobject:DisplayObject = super.addChild(child); 

     arrange(); 

     return displayobject; 

    }// end function 

    override public function addChildAt(child:DisplayObject, 
             index:int):DisplayObject 
    { 
     var displayobject:DisplayObject = super.addChildAt(child, index); 

     arrange(); 

     return displayobject; 

    }// end function 

    override public function removeChild(child:DisplayObject):DisplayObject 
    { 
     var displayobject:DisplayObject = super.removeChild(child); 

     arrange(); 

     return displayobject; 

    }// end function 

    override public function removeChildAt(index:int):DisplayObject 
    { 
     var displayobject:DisplayObject = super.removeChildAt(index); 

     arrange(); 

     return displayobject; 

    }// end function 

    private function arrange():void 
    { 
     if (numChildren > 0) 
     { 
      for (var i:uint = 0; i < numChildren; i++) 
      { 
       getChildAt(i).y = (i > 0) ? getChildAt(i - 1).y + getChildAt(i - 1).height : 0; 

      }// end for 

     }// end if 

    }// end function 

}// end class 

[UPDATE]

以下代碼的每一行:

vGroup.removeChild(blocks[3]); // removes block 4 
vGroup.removeChildAt(0); // removes block 1 
vGroup.addChildAt(getBlock(6, 100, 100),1); // adds block 6 

產生下列各自的輸出(除了其中第一個是初始輸出):

enter image description here

2
var stack:Array = []; 

function render():void { 
    while(this.numChildren) this.removeChildAt(0); // removes all existing children 
    for(var i = 0; i < stack.length; ++i) { 
    var c = stack[i]; 
    c.y = this.height; 
    this.addChild(c); 
    } 
} 

stack.push(num1); 
stack.push(num2); 
stack.push(num3); 
render(); 

很好的例子
stack.splice(1,1); // removes the second item from [stack] 
render(); 

大量了,我只是想補充一個短。請享用。

相關問題