Detect Mouse leave stage while dragging in Actionscript 3

后端 未结 8 621
我寻月下人不归
我寻月下人不归 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: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);
            }
        }
    
    }
    

提交回复
热议问题