Detect Mouse leave stage while dragging in Actionscript 3

后端 未结 8 567
我寻月下人不归
我寻月下人不归 2020-12-05 05:27

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

相关标签:
8条回答
  • 2020-12-05 06:30

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-05 06:33

    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);
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题