2016-05-04 112 views
2

我發生了點擊(移動字符)時,鼠標位置上的事件監聽器(座標)發生此遊戲。actionscript 3,removeEventListener無法正常工作

我有另一個事件監聽器拖放(合併項目),工作得很好。

function stageDown (event:MouseEvent):void 
     {    
      stage.removeEventListener(MouseEvent.CLICK, coordinates); 
      MovieClip(getChildByName(event.target.name).toString()).startDrag(); 
      MovieClip(getChildByName(event.target.name).toString()).addEventListener(MouseEvent.MOUSE_UP,stageUp); 

      ...stuff.. 

     } 

function stageUp(event:MouseEvent):void 
    { 
     stopDrag(); 

     ...stuff... 

     stage.addEventListener(MouseEvent.CLICK, coordinates); 
    } 

在功能stageDown我刪除了事件偵聽運動(座標),比我再次添加它在功能stageUp結束(當你鬆開鼠標按鈕,拖動完成)

但不工作,當我鬆開拖字開始移動,無法理解爲什麼

+0

是附着在舞臺你的'stageDown'處理程序(如名稱可能暗示)?或您正在拖動的項目? – BadFeelingAboutThis

+0

'MovieClip(getChildByName(event.target.name).toString())。startDrag();'哦親愛的! – null

+1

哦,親愛的,猜猜簡單的「event.target.startDrag()」並不夠時髦...... – BotMaster

回答

1

我不完全理解爲什麼(事做如何Click事件跟蹤我猜想),但是這是'正常'的行爲。

以下是我過去如何處理這個問題。基本上你可以添加一個更高優先級的點擊收聽到您拖動對象,並取消該事件有:(見代碼註釋)

//Assuming you have something like this in your code///////// 
stage.addEventListener(MouseEvent.CLICK, coordinates); 

function coordinates(event:MouseEvent):void { 
    trace("STAGE CLICK"); //whatever you do here 
} /////////////////////////////////////////////////////////// 

//add your mouse down listener to your object 
someObject.addEventListener(MouseEvent.MOUSE_DOWN, stageDown); 

//ALSO add a click listener to your object, and add it with higher priority than your stage mouse click listener 
someObject.addEventListener(MouseEvent.CLICK, itemClick, false, 999); 

function itemClick(event:MouseEvent):void { 
    //stop the event from reaching the lower priority stage mouse click handler 
    event.stopImmediatePropagation(); 
    trace("Item CLICK"); 
} 

function stageDown (event:MouseEvent):void 
{    
    Sprite(event.currentTarget).startDrag(); 
    //listen for the mouse up on the stage as sometimes when dragging very fast there is slight delay and the object may not be under the mouse 
    stage.addEventListener(MouseEvent.MOUSE_UP,stageUp, true); 

    //if you don't care about mouse up on stage, then you can just forget the mouse up listener and handler altogether and just stop drag on the itemClick function. 
} 

function stageUp(event:MouseEvent):void 
{ 
    //remove the stage mouse up listener 
    stage.removeEventListener(MouseEvent.MOUSE_UP,stageUp, true); 
    trace("UP"); 
    stopDrag(); 
} 
+0

它像一個魅力一樣工作,非常感謝你! – Stevemaster