Skip to main content
Chipleh
Inspiring
April 7, 2011
Answered

drag and drop interaction - logic flaw

  • April 7, 2011
  • 1 reply
  • 507 views

Hi,

I'm hoping for some help in ironing out this small bit of code. My logic is somehow screwed up.

I have  10 clips and 10 targets. The user drags the clips to the targets, and when dropped on a target, the clip locks in place to the x and y coord of the target.


The code below only allows for the user to drop the clip on its cooresponding target; i.e.:

- droppedClip1 can only be dropped on hitTarget1
- droppedClip2 can only be dropped on hitTarget2
- etc.

How can I modify the code so that the droppedClip can be dropped on any of the 10 targets?

Ty for any help,
~chipleh

//droppedClip = movie clip the user just dropped
//totalClips = total number of draggable clips

function evaluateDroppedClip(droppedClip:Object,totalClips:String):void
{
for (var i:Number = 0;i<Number(totalClips);i++)
{
  var hitTarget:Object = objectContainer.getChildByName("hitTarget" + i);      
  if(droppedClip.hitTestObject(hitTarget)) 
  {          
   trace("hitTarget = " + hitTarget.name);           
   droppedClip.x = hitTarget.x;
   droppedClip.y = hitTarget.y;     
  }else{
   droppedClip.x = droppedClip.xPos;//the original position of the dropped clip
   droppedClip.y = droppedClip.yPos;//the original position of the dropped clip  
  }
}
}


  

This topic has been closed for replies.
Correct answer Ned Murphy

I didn't try to interpret what your extra coding is trying to accomplish.  Your goal from the original posting is to allow a dragged object to be dropped on any target and stick.  So I'd say head back to what you showed originally and start there with what I already offered.  Assign a property to each of your targets, name it "isTarget" or something and assign it a value of true.  Then in the first conditional test if the target's isTarget property is true, and if so, let it land.

1 reply

Ned Murphy
Legend
April 7, 2011

Just get rid of the conditional that tests....

if(droppedClip.hitTestObject(hitTarget))

or if you need to have it only drop on targets, then change that conditional to text if the instance name or a property you assign to the target indicates it is a target and not something else.

Chipleh
ChiplehAuthor
Inspiring
April 7, 2011

Hi Ned,

Thx for the reply. I am actually testing for 5 different conditions. Here's the full code below. Everything works fine except for the problematic the first condition. I tried to get rid of the first condition like you mentioned, but everything gets dropped on the last target. How can I loop through and isolate the individual targets and check if any one of them has a an object that's been dropped on it(if that makes any sense).

Any suggestions?

thx again,

~chipleh

//droppedClip: the clip the user just dropped

//totalClips: the total number of clips to be dragged and dropped

//bottomTarget: a single movie clip defined on the stage

function evaluateDroppedClip(droppedClip:Object, totalClips:String):void
{
for (var i:Number = 0;i<Number(totalClips);i++)
{
  var aTarget:Object = objectContainer.getChildByName("listObject" + i); 
  var hitTarget:Object = objectContainer.getChildByName("hitTarget" + i);      
  if(droppedClip.hitTestObject(hitTarget)) 
  {          
   trace("hitTarget = " + hitTarget.name);        
   droppedClip.x = hitTarget.x;
   droppedClip.y = hitTarget.y;  
  
  }else if (droppedClip.hitTestObject(aTarget))   
  {  
   hitTarget.listNumber.text = hitTarget.listNumber.text;  
   droppedClip.x = droppedClip.xPos;
   droppedClip.y = droppedClip.yPos;
   trace("aTarget");      
  } 
  else if (droppedClip.hitTestObject(hitTarget) && droppedClip.hitTestObject(aTarget))   
  {  
   hitTarget.listNumber.text = hitTarget.listNumber.text;  
   droppedClip.x = droppedClip.xPos;
   droppedClip.y = droppedClip.yPos;
   trace("hitTarget & aTarget");     
  }
  else if(droppedClip.hitTestObject(aTarget) && droppedClip.hitTestObject(bottomTarget))
  {  
   hitTarget.listNumber.text = hitTarget.listNumber.text;  
   droppedClip.x = droppedClip.xPos;
   droppedClip.y = droppedClip.yPos;      
   trace("aTarget & bottomTarget");     
  }
  else if(droppedClip.hitTestObject(aTarget) && droppedClip.hitTestObject(bottomTarget) && droppedClip.hitTestObject(hitTarget))
  {  
   hitTarget.listNumber.text = hitTarget.listNumber.text;  
   droppedClip.x = droppedClip.xPos;
   droppedClip.y = droppedClip.yPos;      
   trace("aTarget & bottomTarget & hitTarget");  
  }  
}    
}

Ned Murphy
Ned MurphyCorrect answer
Legend
April 7, 2011

I didn't try to interpret what your extra coding is trying to accomplish.  Your goal from the original posting is to allow a dragged object to be dropped on any target and stick.  So I'd say head back to what you showed originally and start there with what I already offered.  Assign a property to each of your targets, name it "isTarget" or something and assign it a value of true.  Then in the first conditional test if the target's isTarget property is true, and if so, let it land.