How to create a drag/drop with ActionScript 3.0? Multiple items being dragged to multiple targets

后端 未结 1 1583
北荒
北荒 2021-01-28 07:19

I am creating an interactive paper doll in Adobe Animate and am having some trouble making the drag drop feature work. I have multiple clothing items that will need to have the

相关标签:
1条回答
  • 2021-01-28 07:49

    I'd suggest not using MOUSE_UP on dragged objects. It may fail in few cases. Also, it's a good idea to bring the dragged object to front.

    var Drugs:Array = [flower_mc, yellowdress_mc, bluedress_mc];
    
    for each (var aDrag:MovieClip in Drugs)
    {
        aDrag.buttonMode = true;
        aDrag.useHandCursor = true;
        aDrag.addEventListener(MouseEvent.MOUSE_DOWN, dragObject);
    }
    
    var orig:Point;
    var drag:MovieClip;
    
    function dragObject(e:MouseEvent):void
    {
        drag = e.currentTarget as MovieClip;
        orig = new Point(drag.x, drag.y);
    
        // Bring to front and start drag.
        drag.parent.setChildIndex(drag, drag.parent.numChildren - 1);
        drag.startDrag();
    
        // Handle drop cases.
        stage.addEventListener(Event.MOUSE_LEAVE, dropObject);
        stage.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    }
    
    function dropObject(e:Event):void
    {
        // Stop handling drop cases.
        stage.removeEventListener(Event.MOUSE_LEAVE, dropObject);
        stage.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
    
        // This event goes from stage so we will work
        // with the "drag" reference saved earlier.
    
        // Sanity check in case something went wrong
        // and the reference is empty.
        if (!drag) return;
    
        drag.stopDrag();
    
        var aMatch:DisplayObject = getChildByName("target" + drag.name);
        var aTarget:DisplayObject = drag['dropTarget'];
    
        // This is your condition so I leave it as is.
        if ((aTarget !== null) && (aTarget.parent == aMatch))
        {
            drag.x = aMatch.x;
            drag.y = aMatch.y;
            drag.buttonMode = false;
            drag.useHandCursor = false;
            drag.removeEventListener(MouseEvent.MOUSE_DOWN, dragObject);
        }
        else
        {
            drag.x = orig.x;
            drag.y = orig.y;
        }
    
        // Clean up.
        drag = null;
        orig = null;
    }
    
    0 讨论(0)
提交回复
热议问题