# Drag and Drop w/ multiple hit targets

**URL:** <https://forum.kirupa.com/t/drag-and-drop-w-multiple-hit-targets/256399>\
**Category:** flash\
**Created:** [April 2, 2008, 6:50pm UTC](https://forum.kirupa.com/t/drag-and-drop-w-multiple-hit-targets/256399 "2008-04-02T18:50:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrWallace](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@MrWallace](https://forum.kirupa.com/u/MrWallace)\
**Post date:** [April 2, 2008, 6:50pm UTC](https://forum.kirupa.com/t/drag-and-drop-w-multiple-hit-targets/256399/1 "2008-04-02T18:50:34Z")

</div>

Well, here I am again…STUMPED, LOL!!!

Basically, I’m trying to create something that will let me drag an object (object\_A) onto a hit area (object\_B) and give it the exact same x and y coordinates as object\_B.

I know object\_A has to be a movie clip with a button inside of it and that the code has to go on the button, not the movie (because the ‘drag’ function only works with a handler like on\_press for example).

Anyways, this is the code that I have right now:

on (press) {  
startDrag("\_root.object\_A");  
}  
on (release) {  
stopDrag();  
if (eval(object\_A.\_droptarget) == \_root.object\_B) {  
object\_A.\_visible = false;  
} else {  
object\_A.\_x = x\_480;  
object\_A.\_y = y\_90;  
}

}

The other catch is…object\_A needs to be able to be dropped onto several possible hit targets, not just object\_B (eg. object\_C and object\_D).

Not sure if it makes any sense to anybody but, if you have any ideas they would be greatly appreciated!

---

<div class="post-metadata">

**Author:** ![bh\_pradipta](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@bh\_pradipta](https://forum.kirupa.com/u/bh_pradipta)\
**Post date:** [May 14, 2008, 6:42am UTC](https://forum.kirupa.com/t/drag-and-drop-w-multiple-hit-targets/256399/2 "2008-05-14T06:42:43Z")

</div>

//there are two arrays generated one for drag and the other for drop and you are getting who was dragged and on whom was dropped.

var xPos:Number;  
var yPos:Number;  
var myDragArray:Array = new Array();  
var myDropArray:Array = new Array();  
myDragArray.push(“source1\_mc”);  
myDragArray.push(“source2\_mc”);  
myDragArray.push(“source3\_mc”);  
myDropArray.push(“t1”);  
myDropArray.push(“t2”);  
myDropArray.push(“t3”);  
//drag portion ///////////////////////////////  
for (i=0; i\<myDragArray.length; i++) {  
eval(myDragArray\*).onPress = function() {  
this.startDrag(false);  
this.swapDepths(this.getNextHighestDepth());  
xPos = this.\_x;  
yPos = this.\_y;  
};  
//drop portion /////////////////////////////  
eval(myDragArray\*).onRelease = eval(myDragArray\*).onReleaseOutside=function () {  
this.stopDrag();  
this.swapDepths(this.getNextHighestDepth());  
if (eval(myDragArray\*.\_droptarget) == eval(myDropArray\*)) {// this is the correct  
//if (eval(this.\_droptarget) == eval(myDropArray\*)) {  
trace(“dropped on :”+this.\_droptarget+" Who was dropped :"+this.\_name);  
}  
whereDrop = eval(this.\_droptarget);  
if (whereDrop == undefined) {  
//trace("would call snap original ");//This would call snap original  
snapOriginal(this, xPos, yPos);  
} else {  
//trace("would snap to its destination "+whereDrop);// this would be dopped on the target object.  
snapToObject(this, whereDrop);  
}  
snapToObject();  
break;  
};  
}  
//function to snap at original point. /////////////////////////////  
function snapOriginal(dragName, xPosition, yPosition) {  
//trace(“snap original function :”+dragName+xPosition +yPosition);  
dragName.\_x = xPos;  
dragName.\_y = yPos;  
}  
//function to snap at the object ////////////////////////  
function snapToObject(drag, drop) {  
drag.\_x = drop.\_x;  
drag.\_y = drop.\_y;  
}

---

<div class="post-metadata">

**Author:** ![bh\_pradipta](https://avatars.discourse-cdn.com/v4/letter/b/7c8e57/32.png) [@bh\_pradipta](https://forum.kirupa.com/u/bh_pradipta)\
**Post date:** [May 14, 2008, 12:14pm UTC](https://forum.kirupa.com/t/drag-and-drop-w-multiple-hit-targets/256399/3 "2008-05-14T12:14:51Z")

</div>

do you need buttons to create your drag and drop only.
