0

I am coding a drag and drop application where I can grab an object and then place it over different object containers. When I am dragging the object (keeping the mouse button pressed) and I leave the stage, I can still control the object with the mouse...this is not what I want.

I would like to lose control of the object when the mouse leaves the stage.

I tried to remove the event listener for MOUSE_DOWN on a MOUSE_LEAVE event but nothing.

I also tried to dispatch a MOUSE_UP event on a MOUSE_LEAVE event but it does not work either...it works only if I manually release the mouse button.

Is there any way to override the MOUSE_DOWN event when the user moves the mouse away from the screen but he is still pressing the mouse button??? Any suggestion???

Thanks in advance

Paranoid Android
  • 4,672
  • 11
  • 54
  • 73

4 Answers4

1

Is the stage actually listening to the MOUSE_LEAVE event? In any case , check this article, it may help: http://www.kirupa.com/developer/flashcs3/detecting_when_mouse_leaves_movie.htm

PatrickS
  • 9,539
  • 2
  • 27
  • 31
  • but it is not triggered when I keep pressing the mouse button down. – Paranoid Android Sep 15 '10 at 09:39
  • you're right , same thing in the example i've given you. have you tried loxxy's solutions? this should definitely work, dispatch a MouseUp event or get the stage to dispatch a MouseLeave event when the mouse is out of bounds – PatrickS Sep 15 '10 at 12:06
  • it works as long as I release the mouse button when I am moving the mouse out of bounds. I set x an y limits therefore my object is stuck by the bounds if I leave the stage but I keep pressing down the mouse button.(it does not disappear like the example)... – Paranoid Android Sep 16 '10 at 14:27
1

Here's a couple tricky traps not to fall into :

One bizarre thing is that in Chrome + Firefox, the MOUSE_LEAVE event isn't dispatched for a WMODE of OPAQUE orTRANSPARENT. It just doesn't fire - mouse down or up.

With WINDOW it works fine. That one took me a long time to find out! grr... http://bugs.adobe.com/jira/browse/FP-892


Second, make sure you're using Event for the parameter type for your Event.MOUSE_LEAVE handler and not MouseEvent. If you try to handle MOUSE_LEAVE with e:MouseEvent you'll get an error that you may never see (unless you're using the debug flash player). It's a very easy mistake to make because you're probably pointing all your other handlers to the same method.

Here's what I do: (just call my main endDrag from mouseLeave(e:Event)

stage.addEventListener(MouseEvent.MOUSE_MOVE, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, endDrag);
stage.addEventListener(Event.DEACTIVATE, endDrag);
stage.addEventListener(Event.MOUSE_LEAVE, mouseLeave);

private function mouseLeave(e:Event):void
{
    endDrag(new MouseEvent("MOUSE_LEAVE"));
}

public function endDrag(evt:MouseEvent):void
{
    /// handle end drag
}
Community
  • 1
  • 1
Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
0

When you drag an object outside the flash movie, the object will fire a MOUSE_OUT event. You can listen to this event, use a variable to check if the object is being dragged, and, if so, dispatch a MOUSE_UP event.

some_object.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
some_object.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
some_object.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);

private function mouseOutHandler(e:MouseEvent):void
{
    if (isDragging)
        e.target.dispatchEvent(new MouseEvent(MouseEvent.MOUSE_UP));
}

private function mouseDownHandler(e:MouseEvent):void
{   
    e.target.startDrag();
    isDragging = true;
}

private function mouseUpHandler(e:MouseEvent):void
{   
    e.target.stopDrag();
    isDragging = false;
}
zok
  • 6,065
  • 10
  • 43
  • 65
0

I believe you are talking about the user leaving the flash content altogether with mouse clicked, and when he/she returns it continues the process right?

I suggest, you track the mouse x & y coordinates. Set a condition which triggers the mouse up event handler when the x or y is equal to the stage width or height respectively.

loxxy
  • 12,990
  • 2
  • 25
  • 56
  • I tried to do something like that but the MOUSE_UP event is not triggered if I keep pressing the mouse button down...I guess it is triggered for an instant and then the MOUSE_DOWN event is triggered again... – Paranoid Android Sep 15 '10 at 09:35