1

我想刪除按鈕上的事件按鈕,所以當按下按鈕時,動畫完成,然後再次按下按鈕。但基於我的代碼下面你可以多次按下按鈕,只要你喜歡:刪除eventListener不在按鈕上工作AS3 - 閃光燈

var LeftButt:MovieClip = new left_button(); 
var RightButt:MovieClip = new right_button(); 
var topClip:Sprite = new Sprite(); 
addChild(topClip); 
    LeftButt.addEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "left");}); 
    RightButt.addEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "right");}); 


function clothingApp(event:MouseEvent):void{ 
    topClip.addChild(RightButt); 
    topClip.addChild(LeftButt); 

} 

function moveItems(event:MouseEvent, SlideDirection:String):void{ 

    LeftButt.removeEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "left");}); 
    RightButt.removeEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "right");});  

    trace(SlideDirection); 
} 

因此從技術上講,因爲我從來沒有成立了事件監聽再此代碼應只運行一次。但是,您可以多次按下按鈕,只要你喜歡。

回答

4

如果你想刪除事件監聽器,你不能使用匿名函數來添加它們。

創建一個與匿名函數具有相同功能的包裝函數,並且您會沒事的。

function moveLeft(event:MouseEvent):void 
{ 
    moveItems(event, "left"); 
} 

function moveRight(event:MouseEvent):void 
{ 
    moveItems(event, "right"); 
} 


LeftButt.addEventListener(MouseEvent.MOUSE_UP, moveLeft); 
RightButt.addEventListener(MouseEvent.MOUSE_UP, moveRight); 

LeftButt.removeEventListener(MouseEvent.MOUSE_UP, moveLeft); 
RightButt.removeEventListener(MouseEvent.MOUSE_UP, moveRight); 
+0

所以我把我的addeventlistener移到了我的函數外面,remove仍然在裏面。我仍然可以按我想要的次數按下它。 – Denoteone 2012-02-28 17:11:21

+0

@Denoteone,你不應該將addEventLIstener移出函數,你需要將函數移出addEventListener。 – 2012-02-28 17:26:21

+0

AH-HA(燈泡)我明白你現在在說什麼。將測試並確認它的工作原理。謝謝+1 – Denoteone 2012-02-28 17:39:27