Event.MOUSE_LEAVE is great in Actionscript 3, but it doesn\'t seem to fire if the user is holding their left (or right for that matter) mouse button down.
Is there a
here's what I do:
mc.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
private function onMouseDown(_e:MouseEvent):void
{
mc2.startDrag(params);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stage.addEventListener(Event.MOUSE_LEAVE, onMouseLeave);
stage.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
}
private function onMouseUp(_e:MouseEvent):void
{
ms2.stopDrag();
}
private function onMouseLeave(_e:Event):void
{
mc2.stopDrag();
}
private function onMouseOut(_e:MouseEvent):void
{
if (e.stageX <= 0 || e.stageX >= stage.stageWidth || e.stageY <= 0 || e.stageY >= stage.stageHeight)
{
mc2.stopDrag();
}
}
Here is the right answer. Custom class you pass a DisplayObject to and will drag it till mouse-up or mouse out-of-stage. Customize at will:
package fanlib.gfx
{
import flash.display.DisplayObject;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.ui.Mouse;
public class Drag
{
private var obj:DisplayObject;
private var point:Point = new Point();
private var stg:Stage;
public function Drag(obj:DisplayObject)
{
this.obj = obj;
stg = Stg.Get();
stg.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
stg.addEventListener(MouseEvent.MOUSE_UP, stopDrag);
//stg.addEventListener(Event.MOUSE_LEAVE, stopDrag); // sh*t just won't fire
point.setTo(stg.mouseX, stg.mouseY);
}
private function mouseMove(e:MouseEvent):void {
if (stg.mouseX <= 0 ||
stg.mouseY <= 0 ||
stg.mouseX >= stg.stageWidth ||
stg.mouseY >= stg.stageHeight) {
stopDrag();
return;
}
obj.x += stg.mouseX - point.x;
obj.y += stg.mouseY - point.y;
point.setTo(stg.mouseX, stg.mouseY);
}
public function stopDrag(e:* = null):void {
stg.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
stg.removeEventListener(MouseEvent.MOUSE_UP, stopDrag);
//stg.removeEventListener(Event.MOUSE_LEAVE, stopDrag);
}
}
}