2016-05-04 10 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

は、私は完全に理由を理解していない理由は、理解できない

しかし、働いていない、(イベントをクリックする方法とは何かを、私が思うに追跡されている)が、それは「通常の」行動である。

これは私が過去にこれをどのように処理したかです。基本的にはあなたがドラッグされたオブジェクトに優先度の高いクリックリスナーを追加し、そこにイベントをキャンセルすることができます(コードのコメントを参照してください)

//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

関連する問題