当点击发生时(移动角色),我在鼠标位置上用事件监听器(坐标)制作了这个游戏。
我还有另一个用于拖放(合并项目)的事件监听器,效果非常好。
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中,我删除了移动的事件监听器( coordinates ),而不是在函数stageUp的末尾再次添加它(当你释放鼠标按钮并且拖动完成时)
但是不行,当我释放拖动角色开始移动时,无法理解为什么
答案 0 :(得分: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();
}