Skip to main content
Known Participant
January 6, 2010
Question

Drag and drop with outputs based on the drop positions.

  • January 6, 2010
  • 1 reply
  • 1043 views

I hope someone can help with this question.  I'm trying to make a simulation where a person can drag 2 different objects to any 4 predefined targets on the stage.  Based on the position of the 2 objects there will be a different output text.  I hope I described that clearly.  I would also like the 2 objects to snap to the 4 targets.  Im very new to AS.  Im starting to be able to read it a bit but still can not wright it.

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
January 6, 2010

if your objects are, at least, sprites you can use the startDrag() and stopDrag() methods and dropTarget property of your objects to determine if they're dropped on one of your 4 target objects:

sprite1.addEventListener(MouseEvent.MOUSE_DOWN,startdragF);

sprite1.addEventListener(MouseEvent.MOUSE_UP,stopdragF);

sprite2.addEventListener(MouseEvent.MOUSE_DOWN,startdragF);

sprite2.addEventListener(MouseEvent.MOUSE_UP,stopdragF);

function startdragF(e:MouseEvent){

e.currentTarget.startDrag();

}

function stopdragF(e:MouseEvent){

e.currentTarget.stopDrag();

for(var i:uint=0;i<4;i++){

if(e.currentTarget.dropTarget!=null){

if(e.currentTarget.dropTarget==this["droptarget"+i]){

e.currentTarget.x=this["droptarget"+i].x;

e.currentTarget.y=this["droptarget"+i].y;

break;

}

} else {

// do whatever.  not dropped on an object

}

}

}

Known Participant
January 6, 2010

This is as far as I have been able to make it. I have attached the file below.  I'm only halfway there and this isn't quite what I need. Right now each object has its own target.  I need ether object to be able to snap to ether target. I also need to add 2 more targets.

object1_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
object1_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
object2_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
object2_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);

function pickUp(event:MouseEvent):void {
    event.target.startDrag(true);
      event.target.startDrag(true);
    reply_txt.text = "";
    event.target.parent.addChild(event.target);

}
function dropIt(event:MouseEvent):void {
     event.target.stopDrag();
      var myTargetName:String = "target" + event.target.name;
    var myTarget:DisplayObject = getChildByName(myTargetName);
    if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
         reply_txt.text = "Good Job!";
        event.target.x = myTarget.x;
        event.target.y = myTarget.y;

       
          } else {
        reply_txt.text = "Try Again!";
       
     }
     
}

kglad
Community Expert
Community Expert
January 6, 2010

use the code i suggested.