Problems replicating drag-and-drop with mouse events

前端 未结 1 872
温柔的废话
温柔的废话 2021-01-16 18:27

I want to replicate the standard startDrag/stopDrag events with my own routine to alter things a bit, and I run into some kind of event propagation or bubbling problem. Her

相关标签:
1条回答
  • 2021-01-16 18:43

    Track which target you're concerned about and only listen to events directly from it. So in your mousemove function, check e.target == your object. In this case, the application. You will get stray events bubbling from sub components.

        public static function smoothDrag(pressEvent:MouseEvent, dragTarget:DisplayObject = null, done:Function=null, move:Function = null, parent:DisplayObject = null):void {
            var target:DisplayObject = dragTarget;
            parent = parent || target.parent;
            var eventParent:EventDispatcher = target.stage || parent;
    
            var moveFunc:Function = move;
            var doneFunc:Function = done;
    
            var startPoint:Point = MouseHelpers.pointTo(pressEvent.localX, pressEvent.localY, pressEvent.target as DisplayObject, parent);
            startPoint.x -= target.x
            startPoint.y -= target.y;
    
            var setPosition:Function = function(e:MouseEvent):void 
            {
                e.stopImmediatePropagation();
                var p:Point = MouseHelpers.pointTo(e.localX,e.localY, e.target as DisplayObject, parent);
                target.x = p.x - startPoint.x;
                target.y = p.y - startPoint.y;
    
                if (moveFunc != null) {
                    moveFunc();
                }
            }
    
            var stopMove:Function = function(e:MouseEvent):void {
                e.stopImmediatePropagation();
                eventParent.removeEventListener(MouseEvent.MOUSE_MOVE, setPosition, true);
                eventParent.removeEventListener(MouseEvent.MOUSE_UP, stopMove, true);
                eventParent.removeEventListener(MouseEvent.ROLL_OVER, EventHelpers.stop, true);
                eventParent.removeEventListener(MouseEvent.MOUSE_OVER, EventHelpers.stop, true);
    
                if (doneFunc != null) {
                    doneFunc(e);
                }
            }
            eventParent.addEventListener(MouseEvent.ROLL_OVER, EventHelpers.stop, true, 0, true);
            eventParent.addEventListener(MouseEvent.MOUSE_OVER, EventHelpers.stop, true, 0, true);
            eventParent.addEventListener(MouseEvent.MOUSE_MOVE, setPosition, true, 0, true);
            eventParent.addEventListener(MouseEvent.MOUSE_UP, stopMove, true, 0, true);
        }
    
    
        /**
         *  Translate a point from one object's reference into another. Best used when you have a descendant object x/y and you
         *  want to get that position relative to an ancestor. Uses the localToGlobal/globalToLocal style. 
         **/
        public static function pointTo(fromX:Number, fromY:Number, src:DisplayObject, dest:DisplayObject):Point {
            var p:Point = new Point(fromX, fromY);
            if(src != dest) {
                p = src.localToGlobal(p);
                p = dest.globalToLocal(p);
            }
            return p;
        }       
    
    0 讨论(0)
提交回复
热议问题