Flash AS3 - Drag and drop multiple objects to multiple targets

前端 未结 2 1846
刺人心
刺人心 2021-01-26 10:32

I have multiple objects to drag to multiple targets. I have a code without error. I am using multiple functions. But I wonder if I pass the objects and the specific target with

2条回答
  •  时光说笑
    2021-01-26 11:05

    You should somehow make your draggable objects know their targets, thus when your SWF registers an end drag event, the object that was being dragged would check against its target and if not colliding, then float/jump back. Since your objects derive from MovieClips, it's possible to add custom properties to them without doing any declarations, but be sure to check if there is something in a custom property before using it. Let's say you have assigned each draggable object a desiredTarget as whatever target you need them to be dragged. Then, you can do like this:

    function dropIt(e:MouseEvent):void {
        var desiredTarget:MovieClip=e.target.desiredTarget as MovieClip; // get where this should be placed
        e.target.stopDrag(); // we still need to release the dragged object
        if (!desiredTarget) return; // no target - nothing to do (also helps with debug)
        if (e.target.hitTestObject(desiredTarget)) {
            e.target.buttonMode=false;
            e.target.x=desiredTarget.x;
            e.target.y=desiredTarget.y;
        } else {
            // move dragged object back to starting position
            e.target.x=e.target.startX;
            e.target.y=e.target.startY;
        }
    }
    

提交回复
热议问题