Skip to main content
Participating Frequently
May 18, 2014
Answered

probleme with drag and drop

  • May 18, 2014
  • 1 reply
  • 577 views

hi I have a probleme with drag and drop I have 2 clip and i have 2 target, i want when i drag clip 1 to target 1 , the target 1 not accepte the clip2, so clip 2 can be draged to target 2 . (target 1 can accept clip 1 and clip 2, also target 2 can accepte clip 1 & 2)

// pierre

clip_piere.addEventListener(MouseEvent.MOUSE_DOWN, dep_piere);

clip_piere.addEventListener(MouseEvent.MOUSE_UP, arreter_piere);

clip_piere.buttonMode = true;

var x_piere,y_piere:Number;

x_piere=clip_piere.x;

y_piere=clip_piere.y;

function dep_piere(event:MouseEvent):void

{

  clip_piere.startDrag();

}

function arreter_piere(event:MouseEvent):void

{

  if(place1.hitTestObject(clip_piere) || place2.hitTestObject(clip_piere) ){

  clip_piere.stopDrag();

  clip_piere.removeEventListener(MouseEvent.MOUSE_DOWN, dep_piere);

  }else{

  clip_piere.x=x_piere;

  clip_piere.y=y_piere;

  clip_piere.stopDrag();

  }

}

// ean

clip_ean.addEventListener(MouseEvent.MOUSE_DOWN, dep_ean);

clip_ean.addEventListener(MouseEvent.MOUSE_UP, arreter_ean);

var x_ean,y_ean:Number;

x_ean=clip_ean.x;

y_ean=clip_ean.y;

function dep_ean(event:MouseEvent):void

{

  clip_ean.startDrag();

}

function arreter_ean(event:MouseEvent):void

{

  if(place1.hitTestObject(clip_ean) || place2.hitTestObject(clip_ean) ){

  clip_ean.stopDrag();

  clip_ean.removeEventListener(MouseEvent.MOUSE_DOWN, dep_ean);

  }else{

  clip_ean.x=x_ean;

  clip_ean.y=y_ean;

  clip_ean.stopDrag();

  }

}

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

I understand you but witch property i have to assign "name" "text" ... and my target is textfield, plz help me  I am a beginner in as3.


Here is a link to a file I made for you that is based on your objects and what I described in my last response.  I have reduced the code so that the
objects share the same functions.  I moved the MOUSE_UP listener to the stage so that it is less prone to fail should you slide off the object when dragging and dropping.

http://www.nedwebs.com/Flash/AS3_Drag_Targets.fla

1 reply

Ned Murphy
Legend
May 18, 2014

You should be using the dropTarget property of the dragged object to determine if it has been dropped on an object (you often have to target the parent of the dropTarget because it will be the child object inside the object you drop on).

Then what you can do is assign a status property to the targets to indficate whether or not they are used already. and use that in your logic to check if a target is open or not  That way, when you drop an object on one and it is valid, you designate the target as being used so that when you drop another you don't qallow it to stay.

Participating Frequently
May 18, 2014

but how can i check if a target is open or not?  the code plz

Ned Murphy
Legend
May 18, 2014

I already explained that.  You assign a properrty to each target, call it "used" or "occupied" or something like that and set it to false initially.  When you try to drop something on the target you check to see if the used property is true or false.  If it is false you allow the drop and then set that property to be true.  That way the next object you drop on it will find the used property to be true and will not allow it to drop there.