2010-02-12 156 views
0

好吧,這已經讓我在過去的幾個小時裏瘋了,我知道有一個簡單的答案。刪除影片剪輯as3

我有一個scrollPane,它有一個名爲right_container_mc的影片剪輯,因爲它是源代碼。 在這個right_container_mc裏面,我還有其他一些名爲execiseBox的影片剪輯,它們從具有for循環的數組中被添加(在舞臺上的正確位置)。 每個練習框都有一個名爲close_btn的按鈕符號。

首先,我不確定這是達到此目的的最佳方法,因此請隨時提出更好的方法!

我想要做的是,當這個close_btn被點擊時,從數組中刪除特定的exerciseBox movieclip,然後從舞臺中再次循環數組,以便所有練習框動畫片段更新它們在舞臺上的位置。

我無法獲取對影片剪輯的引用,因爲它是嵌套的,以便將它從數組和階段中刪除。這是我迄今爲止的代碼,需要添加刪除和更新部分。另外我應該在數組循環每次運行之前刪除所有當前的exerciseBox影片剪輯的實例?

任何幫助,非常感謝。

function addMovieClipsToStage(event:MouseEvent):void 
    { 
    scrollPaneRight.source = right_container_mc; 
    exerciseBox = new Exercisebox(); 
    exerciseBox.close_btn.addEventListener(MouseEvent.CLICK, onRemoveBox); 
    boxArray.push(exerciseBox); 
    sortBoxes(); 
    scrollPaneRight.update(); 
    } 

    function onRemoveBox(event:MouseEvent):void 
    { 

    } 

    function sortBoxes():void 
    { 
    for (var i:int =0; i<boxArray.length; i++) 
    { 
    right_container_mc.addChild(exerciseBox); 
    exerciseBox.x = 0; 
    exerciseBox.y = ((115 + 3)*i); 

    } 

    } 

回答

1

在onRemoveBox功能

event.currentTarget應該返回函數被觸發的對象。

眼看作爲對象right_container_mc的孩子,也許你可以試試:

right_container_mc.removeChild(event.currentTarget); 
基於你已經發布的代碼

我不知道,你甚至需要一個數組。看起來你正在用它來跟蹤孩子的數量。顯示列表已經爲你做了這個。

所以我認爲你的排序可能只是參考right_container_mc.numChildren而不是數組長度。

希望這有助於一些!

+0

我得到以下編譯器錯誤: 1118:靜態類型對象的值隱式強制爲可能無關的類型閃存。顯示:的DisplayObject。 – 2010-02-12 18:21:09

+0

好吧,我剛剛在event.currentTarget上做了一個跟蹤,它顯示了簡單的按鈕。我將上面的內容更改爲right_container_mc.removeChild(event.currentTarget.parent);這似乎有效。現在我只需要獲取剩餘的影片剪輯即可更新位置 – 2010-02-12 18:28:42

0

爲了擺脫boxArray,你可以循環遍歷right_container_mc中的所有練習框。

function addMovieClipsToStage(event:MouseEvent):void 
{ 
    scrollPaneRight.source = right_container_mc; 
    exerciseBox = new Exercisebox(); 
    exerciseBox.close_btn.addEventListener(MouseEvent.CLICK, onRemoveBox); 
    right_container_mc.addChild(exerciseBox); 
    sortBoxes(); 
    scrollPaneRight.update(); 
} 

function onRemoveBox(event:MouseEvent):void 
{ 
    right_container_mc.removeChild(event.currentTarget); 
    sortBoxes(); 
} 

function sortBoxes():void 
{ 
    var count:int = 0; 
    for each(var exerciseBox:Exercisebox in right_container_mc) 
    { 
     count++; 
     exerciseBox.x = 0; 
     exerciseBox.y = (115 + 3) * count; 
    } 
} 

有關「每個...在」更多信息,請http://help.adobe.com/en_US/AS3LCR/Flash_10.0/statements.html#for_each..in

0

出於某種原因,在sortBoxes功能每個循環不點火。我添加了一個跟蹤語句來檢查這一點,並沒有任何反應,這裏是更新後的代碼:

public function addMovieClipsToStage(event:MouseEvent):void 
      { 
       scrollPaneRight.source = right_container_mc; 
       exerciseBox = new Exercisebox(); 
       exerciseBox.close_btn.addEventListener(MouseEvent.CLICK, onRemoveBox); 
       exerciseBox.x = 0; 
       exerciseBox.y = (118 * exerciseBoxAmt); 
       right_container_mc.addChild(exerciseBox); 
       exerciseBoxAmt++; // the position of the boxes added to stage variable 
       sortBoxes(); 
       scrollPaneRight.update(); 
      } 

      public function sortBoxes():void 
      { 
       var count:int = 0; 
       for each (var exerciseBox:Exercisebox in right_container_mc) 
       { 

        exerciseBox.x = 0; 
        exerciseBox.y = (118 * count); 
        count++; 
        trace(count); //does not display in output window!! 
       } 
      } 


      public function onRemoveBox(event:MouseEvent):void 
      { 
       right_container_mc.removeChild(event.currentTarget.parent); 
       exerciseBoxAmt--; 
       sortBoxes(); 

      }