1

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?

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • how and what are you dragging? should the draggable object be moved only with mouse over it or it goes after the cursor wherever it's moved with the left btn pressed? i'm asking because imho for most cases just a `MouseEvent.MOUSE_MOVE` listener with an `event.buttonDown` check is the best practice – www0z0k Dec 06 '10 at 23:05
  • @www0z0k apparently you didn't even read my code. I would give you -1 on the comment if I could. I already have the basics of the drag down **that is not the issue** the issue is when some extraneous event fires that prevents the `MOUSE_UP` event from being caught by flash. – zzzzBov Dec 06 '10 at 23:10
  • i'd better post an answer ;) `Event.MOUSE_LEAVE` is dispatched when the context menu is called, when the mouse with left btn released leaves the stage or when left btn is released when mouse is off the stage – www0z0k Dec 06 '10 at 23:30

3 Answers3

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

www0z0k
  • 4,444
  • 3
  • 27
  • 32
  • @www0z0k your code works **worse** than what I'm *already using*. If I hold the left button down while on the stage, and then click the right mouse button, the context menu pops up, and then cancel the context menu (clicking outside the stage) the blue object continues to be attached to the mouse, despite no longer holding the left mouse button. It is that specific type of issue I was asking to fix. – zzzzBov Dec 07 '10 at 00:14
  • @zzzzBov currently trying to fix it. anything else wrong in my code? – www0z0k Dec 07 '10 at 01:37
  • @zzzzBov updated my code with a couple of events, could you pls check it? – www0z0k Dec 07 '10 at 02:01
  • @www0z0k you haven't provided any *new* events that can interrupt the handling of a MOUSE_UP event. I already know how to do dragging. All I was asking was for someone to provide a list of **OTHER** events that might cause issues. `MOUSE_LEAVE` doesn't cause issues, `FOCUS_OUT` might, if you can provide some examples of how it triggers in a way that prevents the `MOUSE_UP` event from firing. I don't understand why anyone would have upvoted your answer as it *doesn't answer my question at all* – zzzzBov Dec 07 '10 at 14:52
  • @ zzzzBov `Event.DEACTIVATE` and i wasn't able to get the issue on wonderfl – www0z0k Dec 07 '10 at 15:23
  • @ zzzzBov Event.DEACTIVATE didn't help? – www0z0k Dec 15 '10 at 04:17
0

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

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
-1

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...
    }
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • You have obviously misunderstood the purpose of `MOUSE_LEAVE`. if a user is holding down the mouse, the mouse_move event typically (there's an issue if wmode is opaque or transparent) continues to fire, which allows you to keep dragging despite the mouse no longer being over the stage. My question was specifically about interrupting the `MOUSE_UP` event, and *not* about `MOUSE_LEAVE`. – zzzzBov May 13 '11 at 13:00
  • @zzzzBov - so don't you mean I misunderstood your question ;-) i think what i said about MOUSE_LEAVE is true – Simon_Weaver May 17 '11 at 02:40
  • `MOUSE_LEAVE` was designed to *not* fire when you leave the stage with the mouse pressed. It will fire when the mouse is released (with the exception of the wmode error in some browsers). It's not designed as a `MOUSE_OUT` substitute. You may note that it's not a `MouseEvent`. – zzzzBov May 17 '11 at 13:09