What flash events can interrupt a mouse_up event, and how do I detect them?
The simplified code:
//triggered on MouseEvent.MOUSE_DOWN
private function beginDrag(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
stage.addEventListener(Event.DEACTIVATE, endDrag);
contextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, endDrag);
}
private function drag(e:MouseEvent):void
{
//do stuff
}
private function endDrag(e:Event):void
{
stage.r开发者_Python百科emoveEventListener(MouseEvent.MOUSE_MOVE, drag);
stage.removeEventListener(MouseEvent.MOUSE_UP, endDrag);
stage.removeEventListener(Event.DEACTIVATE, endDrag);
contextMenu.removeEventListener(ContextMenuEvent.MENU_SELECT, endDrag);
}
I am using some click-and-drag techniques within my flash code, and I've noticed some loopholes with the MOUSE_UP event:
- it wont be triggered if a context menu is activated while the mouse is still held down.
- it wont be triggered if the window is deactivated (alt+tab or similar)
My question is: What other events can possibly interrupt the MOUSE_UP event and lead to unexpected behavior?
Additionally is there a way to generically catch ContextMenuEvent.MENU_SELECT for all context menus without having to manually add/remove the listeners to each context menu?
this code might help
i commented out everything unnesesary with /* */
you are very welcome to upgrade that code if it doesn't fit your situation
It may be possible for the Event.REMOVED_FROM_STAGE
or Event.REMOVED
events to fire if the compiled swf is a child of another swf. I believe in that scenario, the owning document's stage is referenced, and therefor still not an issue.
FocusEvent.FOCUS_OUT
doesn't trigger until after the user has let go of the mouse, which would trigger the MouseEvent.MOUSE_UP
event.
It seems my original code works well enough. (there is the possibility of a loophole if a sub-element's context menu is triggered).
There's a big problem with MOUSE_LEAVE
: if you have the mouse held down then MOUSE_LEAVE
is not fired.
This is what I do to simulate MOUSE_LEAVE
during a drag. Fortunately the stage.mouseX
and stage.mouseY
are updated while the mouse is still down. You probably need MOUSE_LEAVE
too for certain browsers.
public function beginDrag(evt:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
stage.addEventListener(Event.DEACTIVATE, endDrag);
stage.addEventListener(Event.MOUSE_LEAVE, endDrag);
_dragging = true;
}
public function drag(evt:MouseEvent):void
{
// check if mouse has fallen off stage
if (stage.mouseX < 0 ||
stage.mouseY < 0 ||
stage.mouseX > stage.stageWidth ||
stage.mouseY > stage.stageHeight)
{
endDrag(evt);
ExternalInterface.call("alert", "Dropped off");
return;
}
// do drag stuff here...
}
精彩评论