如何使用ActionScript 3.0创建拖放?将多个项目拖动到多个目标

时间:2017-03-01 20:36:38

标签: actionscript-3 events drag-and-drop

我正在Adobe Animate中创建一个交互式纸质玩偶,并且在拖动拖放功能工作方面遇到了一些麻烦。我有多个衣物需要被拖到同一个区域。例如,我可能有4种不同的衬衫选择。我已经开始使用这两件连衣裙(黄色dress_mc和蓝色dress_mc)。黄色连衣裙工作正常,但蓝色连衣裙不起作用,我可以拖动它,但不能掉下来。有什么建议吗?

这是我现在的代码:

var objectoriginalX:Number;
var objectoriginalY:Number;

flower_mc.buttonMode = true;
flower_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
flower_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);

yellowdress_mc.buttonMode = true;
yellowdress_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
yellowdress_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);

bluedress_mc.buttonMode = true;
bluedress_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
bluedress_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);

function pickupObject(event:MouseEvent):void {
event.target.startDrag(true);
event.target.parent.addChild(event.target);
objectoriginalX = event.target.x;
objectoriginalY = event.target.y;
}
function dropObject(event:MouseEvent):void {
event.target.stopDrag();
var matchingTargetName:String = "target" + event.target.name;
var matchingTarget:DisplayObject = getChildByName(matchingTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == 
matchingTarget){
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
event.target.buttonMode = false;
event.target.x = matchingTarget.x;
event.target.y = matchingTarget.y;
} else {
event.target.x = objectoriginalX;
event.target.y = objectoriginalY;
}
}

1 个答案:

答案 0 :(得分:1)

我建议不要在拖动的对象上使用MOUSE_UP。在少数情况下可能会失败。此外,将拖动的物体放在前面是一个好主意。

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;
}