What flash events can interrupt a mouse_up event, and how do I detect them?

你说的曾经没有我的故事 提交于 2019-12-11 07:08:15

问题


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.removeEventListener(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?


回答1:


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




回答2:


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).




回答3:


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...
    }


来源:https://stackoverflow.com/questions/4371731/what-flash-events-can-interrupt-a-mouse-up-event-and-how-do-i-detect-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!