# Drag and drop randomly moving obj

**URL:** https://forum.kirupa.com/t/drag-and-drop-randomly-moving-obj/223427
**Category:** Uncategorized
**Created:** [April 22, 2007, 5:54pm UTC](https://forum.kirupa.com/t/drag-and-drop-randomly-moving-obj/223427 "2007-04-22T17:54:32Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![pamiro](https://avatars.discourse-cdn.com/v4/letter/p/c67d28/32.png) [@pamiro](https://forum.kirupa.com/u/pamiro)
#### Post date: [April 22, 2007, 5:54pm UTC](https://forum.kirupa.com/t/drag-and-drop-randomly-moving-obj/223427/1 "2007-04-22T17:54:32Z")

</div>

hi all ,  
i am trying to do a drag and drop game .  
i want to have some multiple objects flying randomly around the scene and i want the user to drag and drop them into a box …  
when the user drags the same color object for three times into the box  
he wins a prize related to that color .

i have solved the drag and drop part like this (thanks to kirupa!!);

on (press) {  
startDrag("\_root.green" ![](http://www.were-here.com/forum/smilies/wink.gif) ;  
}  
on (release) {  
stopDrag();  
if (\_root.green.\_droptarget == “/computer” ![](http://www.were-here.com/forum/smilies/wink.gif) {  
\_root.computer.gotoandStop(“green” ![](http://www.were-here.com/forum/smilies/wink.gif) ;  
\_root.pieceCount += 1;  
if (\_root.pieceCount == 3) {  
\_root.prize.gotoAndStop(“you won the prize nr 5” ![](http://www.were-here.com/forum/smilies/wink.gif) ;  
}  
}  
}

so if the user drags the same object for 3 times onto the box he wins a prize related to that object .

and to do the game more challanging i want to make the object to move randomly around the stage ;  
so i added this code on the objects ;

onClipEvent (load) {

width = 800;  
height = 600;  
speed = Math.round(Math.random()\*2)+1;  
//initial positions  
x = Math.random()\*width;  
y = Math.random()\*height;  
this.\_x = x;  
this.\_y = y;  
x\_new = Math.random()\*width;  
y\_new = Math.random()_height;  
}  
onClipEvent (enterFrame) {  
//x movement  
if (x\_new\>this.\_x) {  
sign\_x = 1;  
} else {  
sign\_x = -1;  
}  
dx = Math.abs(x\_new-this.\_x);  
if ((dx\>speed) || (dx\<-speed)) {  
this.\_x += sign\_x_speed;  
} else {  
x\_new = Math.random()_width;  
}  
//y movement  
if (y\_new\>this.\_y) {  
sign\_y = 1;  
} else {  
sign\_y = -1;  
}  
dy = Math.abs(y\_new-this.\_y);  
if ((dy\>speed) || (dy\<-speed)) {  
this.\_y += sign\_y_speed;  
} else {  
y\_new = Math.random()\*height;  
}  
}

the problem,  
both codes work ok when they are seperated …  
but together the drag and drop doesnt work … after you click the object remains attaced to the mouse in a strange way …

any ideas please…
