Amirhosein
Amirhosein

Reputation: 4446

Drag and drop in Actionscript 3

I have 2 objects and 1 target. I'm going to drag one object when mouse key is down and drop it when mouse key up and the object be on the one of the target. When o2 is in target and drag o1 to the target, o2 goes to its place instead o1 doesn't go to target.

Could someone help me?

 var t1:int=0; //target is empty

 o1.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
 o1.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

 function startDragging(evt:MouseEvent):void
 {
o1.startDrag();
if (o1.x == tar1.x && o1.y == tar1.y)
{
    t1 = 0;
}
}
function stopDragging(evt:MouseEvent ):void
  {
    if (tar1.hitTestObject(o1)  && t1 == 0)
    {
        o1.x = tar1.x;
        o1.y = tar1.y;
        o1.stopDrag();
        t1 = 1; //target is full
    }
        else
    {
        o1.x = 250;
        o1.y = 200;
        o1.stopDrag();
    }
        }

   o2.addEventListener(MouseEvent.MOUSE_DOWN, startDragging2);
   o2.addEventListener(MouseEvent.MOUSE_UP, stopDragging2);

   function startDragging2(evt:MouseEvent):void
    {
o2.startDrag();
if (o2.x == tar1.x && o2.y == tar1.y)
{
    t1 = 0;
}
}
function stopDragging2(evt:MouseEvent ):void
   {
    if (tar1.hitTestObject(o2)   && t1 == 0)
    {
        o2.x = tar1.x;
        o2.y = tar1.y;
        o2.stopDrag();
        t1 = 1;}
        else 
    {
        o2.x = 250;
        o2.y = 140;
        o2.stopDrag();
    }
        }           

enter image description here

Upvotes: 0

Views: 1015

Answers (1)

Vesper
Vesper

Reputation: 18757

The trick is, your o2 object receives first MouseEvent.ROLL_OVER event while you're dragging o1, and then receives MOUSE_UP event as well as o1. Then, both event listeners trigger separately. First listener stopDragging activates, checks for t1 "global" variable, finds it being 0 - OK, o1 is set over target, and then stopDragging2 triggers, bam, t1 is nonzero, so else branch triggers and o2 gets replaced back to starting position. The normal solution is adding and removing listeners from objects of yours, and addressing objects via target property of an event, like this:

o1.addEventListener(MouseEvent.MOUSE_DOWN,startDragging);
o2.addEventListener(MouseEvent.MOUSE_DOWN,startDragging);
// note that listeners are not separate functions! We will
// be using event.target to check which object is being dragged
function startDragging(e:MouseEvent):void {
    e.target.removeEventListener(MouseEvent.MOUSE_DOWN,startDragging);
    // surprise! This object will not be moved via MOUSE_DOWN,
    // because it's already being moved
    e.target.addEventListener(
    e.target.startDrag(); // drag
} // no alteration to t1, as we don't really care if we're dragging 
// our object off target - yet
function stopDraggging(e:MouseEvent):void {
    e.target.stopDrag(); // stop dragging anyway
    e.target.removeEventListener(MouseEvent.MOUSE_UP,stopDragging);
    // and stop listening for mouse-ups
    if (e.target.hitTestObject(tar1)&& (t1==0)) {
        e.target.x=tar1.x;
        e.target.y=tar1.y;  
        t1=1; // target is full
        return; // emergency exit. We don't need to do more
    }
    // should you add more targets, you can put hitTest code here

    // if we reached this point, we don't hit any targets
    // so we should put our objects back into their positions. 
    // The best way would be storing their base coordinates on 
    // objects themselves, so we don't have to run elsewhere for them
    e.target.x=e.target.startingX; // make sure these are filled
    e.target.y=e.target.startingY;
    e.target.addEventListener(MouseEvent.MOUSE_DOWN,startDragging);
    // and enable this object to be dragged again.
}

Upvotes: 1

Related Questions