Skip to main content
Participant
July 16, 2023
Question

Drag and Drop with Multiple Targets

  • July 16, 2023
  • 2 replies
  • 211 views

Hello, I am working on a drag and drop animation in Adobe Animate Actionscript 3.0. There will be 3 Do's targets and 4 Don'ts targets. There will be 3 draggable objects that can go in ANY of the Do's targets and 4 draggable objects that can go in ANY of the Don'ts targets. The drag function works but when I drop the object, it goes to the upper left corner of the screen, but I don't know what to change. If someone could please help me, I would greatly appreciate it. Here is my code: 

 

var draggableObjects:Array = [TapWaterDD_mc, ReplaceCaseDD_mc, NewSolutionDD_mc, DryCaseDD_mc, ShowerDD_mc, SwimDD_mc, SleepDD_mc];
var doTargets:Array = [targetDos1_mc, targetDos2_mc, targetDos3_mc];
var dontsTargets:Array = [targetDonts1_mc, targetDonts2_mc, targetDonts3_mc, targetDonts4_mc];
var objectsCount:int = draggableObjects.length;
var intargetsCount:int = 0;

// Enable dragging for draggable objects
for each (var draggableObj:MovieClip in draggableObjects) {
draggableObj.buttonMode = true;
draggableObj.addEventListener(MouseEvent.MOUSE_DOWN, pickupobject5);
draggableObj.addEventListener(MouseEvent.MOUSE_UP, dropobject5);
}

function pickupobject5(event:MouseEvent):void {
event.target.startDrag(true);
event.target.parent.addChild(event.target);
}

function dropobject5(event:MouseEvent):void {
event.target.stopDrag();

var matchingTarget:MovieClip;
var columnTargets:Array;

if (draggableObjects.indexOf(event.target) != -1) {
columnTargets = doTargets;
} else {
columnTargets = dontsTargets;
}

for each (var target:MovieClip in columnTargets) {
if (event.target.hitTestObject(target)) {
matchingTarget = target;
break;
}
}

if (matchingTarget != null) {
if (matchingTarget.numChildren == 0) {
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickupobject5);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropobject5);
event.target.buttonMode = false;
matchingTarget.addChild(event.target as MovieClip);
event.target.x = matchingTarget.x;
event.target.y = matchingTarget.y;
intargetsCount++;
if (intargetsCount == objectsCount) {
advancetonextframe5();
}
} else {
// If the target already has a draggable object, return it to its original position
event.target.x = event.target.parent.x;
event.target.y = event.target.parent.y;
}
} else {
// If dropped outside any target, return the draggable object to its original position
event.target.x = event.target.parent.x;
event.target.y = event.target.parent.y;
}
}

function advancetonextframe5():void {
gotoAndStop(135);
}

This topic has been closed for replies.

2 replies

kglad
Community Expert
Community Expert
July 16, 2023

use the trace function to see what you're doing.

kglad
Community Expert
Community Expert
July 16, 2023

in the future, to find the best place to post your message, use the list here, https://community.adobe.com/

p.s. i don't think the adobe website, and forums in particular, are easy to navigate, so don't spend a lot of time searching that forum list. do your best and we'll move the post if it helps you get responses.



<"moved from using the community">