2011-04-11 127 views
0

我只是在學習ActionScript 3,所以這對你的親們來說可能是一個簡單的問題。 我使用按鈕創建了我想添加到舞臺(從庫中)的影片剪輯。我總共有六個按鈕(和六個moviclips),但我試圖在開始下一個按鈕之前讓前兩個按鈕工作。問題是,我似乎無法刪除當另一個按鈕被點擊或相同的按鈕時刪除影片剪輯...就像我說我是新來的,我認爲創造了一堆東西......我得到錯誤2007現在ActionScript 3 - 添加和刪除舞臺上的孩子

import flash.events.MouseEvent; 
import flash.display.MovieClip; 

mix_btn.addEventListener(MouseEvent.CLICK, addbear); 
function addbear(event:MouseEvent):void 
{ 
    var movieClip:bear1 = new bear1(); 
    addChild(movieClip); 
    movieClip.x = 240; 
    movieClip.y = 45; 


mix_btn.removeEventListener(MouseEvent.CLICK, addbear); 
mix_btn.addEventListener(MouseEvent.CLICK, removebear); 
} 
function removebear(event:MouseEvent):void 
{ 
    var movieClip:bear1 = null; 
    removeChild(movieClip); 
    mix_btn.removeEventListener(MouseEvent.CLICK, removebear); 
/* mix_btn.addEventListener(MouseEvent.CLICK, addbear);*/ 
} 
shake_btn.addEventListener(MouseEvent.CLICK, addbear3); 
function addbear3(event:MouseEvent):void 
{ 
    var movieClip:bear3 = new bear3(); 
    addChild(movieClip); 
    movieClip.x = 240; 
    movieClip.y = 45; 

    shake_btn.removeEventListener(MouseEvent.CLICK, addbear3); 
    shake_btn.addEventListener(MouseEvent.CLICK, removebear3); 
} 
function removebear3(event:MouseEvent):void 
{ 
    var movieClip:bear3 = null; 
    removeChild(movieClip); 
    shake_btn.removeEventListener(MouseEvent.CLICK, removebear3); 
    shake_btn.addEventListener(MouseEvent.CLICK, addbear3); 
} 

回答

2
var movieClip:bear1 = null; 
removeChild(movieClip); 
mix_btn.removeEventListener(MouseEvent.CLICK, removebear); 

這裏這部分,你基本上嘗試刪除一個空對象,它通常是不好的:)。這適用於「蛇」和其他按鈕或動畫片段。如果你想刪除您按下了熊,試試這個:

var movieClip:bear1 = bear1(event.currentTarget); 
removeChild(movieClip); 
mix_btn.removeEventListener(MouseEvent.CLICK, removebear); 
0

的問題是,如果引用的動畫片段作爲一個函數級別的變量,它消失時功能齊全。當您的刪除功能稍後發生時,動畫片段本身仍然存在,但用於引用它的動畫片段變量已消失。

如果刪除事件直接在熊上,您可以使用event.currentTarget來訪問並刪除它。

相反,由於您的偵聽器看起來像一個單獨的按鈕,因此您需要引用可在任何地方使用的動畫片段。如果您將實例名稱分配給動畫片段,則可以稍後使用它來訪問和刪除它。

function addbear(event:MouseEvent):void 
{ 
    var movieClip:bear1 = new bear1(); 
    addChild(movieClip); 
    movieClip.x = 240; 
    movieClip.y = 45; 
    movieClip.name = "bear1"; 
} 

function removebear(event:MouseEvent):void 
{ 
    removeChild(getChildByName("bear1")); 
} 
+0

只要將其添加到顯示列表中,它就不能「消失」。不僅如此,他使用2個不同的局部變量,所以當然不是這樣。 – 2011-04-11 17:00:53

0

這完全是一個範圍問題 在你的函數addbear您創建一個變種作用域此函數調用的動畫片段

mix_btn.addEventListener(MouseEvent.CLICK, addbear); 
function addbear(event:MouseEvent):void 
{ 
    var movieClip:bear1 = new bear1(); 
    addChild(movieClip); 
    movieClip.x = 240; 
    movieClip.y = 45; 


mix_btn.removeEventListener(MouseEvent.CLICK, addbear); 
mix_btn.addEventListener(MouseEvent.CLICK, removebear); 
} 
function removebear(event:MouseEvent):void 
{ 
    var movieClip:bear1 = null; 
    removeChild(movieClip); 
    mix_btn.removeEventListener(MouseEvent.CLICK, removebear); 
/* mix_btn.addEventListener(MouseEvent.CLICK, addbear);*/ 
} 

糾正這一點,你需要移動mopvieClip範圍的功能外

mix_btn.addEventListener(MouseEvent.CLICK, addbear); 
    var movieClip:bear1 // **********************global scoping 
    function addbear(event:MouseEvent):void 
    { 
     movieClip = new bear1(); // *************constructing new GLOBAL object here 
     addChild(movieClip); 
     movieClip.x = 240; 
     movieClip.y = 45; 


    mix_btn.removeEventListener(MouseEvent.CLICK, addbear); 
    mix_btn.addEventListener(MouseEvent.CLICK, removebear); 
    } 
    function removebear(event:MouseEvent):void 
    { 
     removeChild(movieClip);//****************** remove movieClip first 
     movieClip= null; // **********************destroying GLOBAL object here 
     mix_btn.removeEventListener(MouseEvent.CLICK, removebear); 
    /* mix_btn.addEventListener(MouseEvent.CLICK, addbear);*/ 
    } 
相關問題