• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Drag and Drop with Multiple Targets

New Here ,
Jul 15, 2023 Jul 15, 2023

Copy link to clipboard

Copied

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

Views

118

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 16, 2023 Jul 16, 2023

Copy link to clipboard

Copied

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">

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 16, 2023 Jul 16, 2023

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines