0
votes

The addEventListener for MOUSE_UP doesn't work -> anybody know whats wrong? It works if I remove the enter_frame line

1

1 Answers

1
votes

This is a pretty common pattern in Flash for when you're doing drag/drop stuff. Basically what is going on is that if you move the target out from under the mouse cursor (or if you move the mouse cursor out from over the target) then the MouseUp event never fires.

The easiest solution, and one I've used frequently, is to change the target of your MouseUp event listener. Rather than listening on the item you're trying to drag, listen on stage instead.

The alternative, and this is desired behavior in some cases, is to listen to both MouseEvent.MOUSE_UP and MouseEvent.MOUSE_OUT on your target item. This way you can stop the drag immediately if the mouse ever leaves that item.

draggableItem.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);

function startDragging(e:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
// OR:
draggableItem.addEventListener(MouseEvent.MOUSE_OUT, stopDragging);
draggableItem.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

draggableItem.startDrag() // etc
}

Does that make sense? Let me know if that helps!