【问题标题】:actionscript 3, removeEventListener not working properlyactionscript 3,removeEventListener 无法正常工作
【发布时间】:2016-05-04 13:40:41
【问题描述】:

当点击发生(移动角色)时,我在鼠标位置上使用事件监听器(坐标)制作了这个游戏。

我还有另一个用于拖放(组合项目)的事件监听器,效果很好。

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 的末尾再次添加它(当您释放鼠标按钮并且拖动完成时)

但是不工作,当我松开拖动时角色开始移动,不明白为什么

【问题讨论】:

  • 您的stageDown 处理程序是否已附加到舞台(顾名思义)?或您正在拖动的项目?
  • MovieClip(getChildByName(event.target.name).toString()).startDrag(); 天哪!
  • 哦,天哪,猜猜简单的“event.target.startDrag()”还不够时髦……
  • @Stevemaster - 你明白了吗?
  • @BotMaster xD 天哪,真可惜,我对 as3 完全是菜鸟。更正谢谢!

标签: actionscript-3 drag-and-drop event-listener


【解决方案1】:

我不完全理解原因(我想这与点击事件的跟踪方式有关),但这是“正常”行为。

这是我过去的处理方式。基本上,您可以为您正在拖动的对象添加更高优先级的单击侦听器并在那里取消事件:(参见代码 cmets)

//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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    • 1970-01-01
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    相关资源
    最近更新 更多