12 Replies Latest reply on May 25, 2016 12:35 PM by mhunter

    drag and drop issue

    mhunter Level 1

      I have two drag objects, drag1 and drag2, two targets, target1 and target2. It works if you do it right, but if I drag drag2 to target1, it snaps to target2. The only error I have. Is this way of coding even workable for multiple targets? Any help would be greatly appreciated...

       

      var startPosition1: Point;
      var startPosition2: Point;

       

      drag1.buttonMode = true;

      drag1.addEventListener(MouseEvent.MOUSE_DOWN, first);

       

      function first(event: MouseEvent): void {
            drag1.startDrag();

            startPosition1 = new Point(drag1.x, drag1.y);
      }

       

      drag1.addEventListener(MouseEvent.MOUSE_UP, second);

       

      function second(event: MouseEvent): void {
            drag1.stopDrag();

            if (drag1.hitTestObject(target1)) {
             drag1.x = target1.x;
             drag1.y = target1.y;
             drag1.buttonMode = false;
             drag1.stopDrag();

      } else {
               drag1.x = startPosition1.x;
               drag1.y = startPosition1.y;
      }
      startPosition1 = null;
      }

       

      drag2.buttonMode = true;

      drag2.addEventListener(MouseEvent.MOUSE_DOWN, third);

       

      function third(event: MouseEvent): void {
              drag2.startDrag();

              startPosition2 = new Point(drag2.x, drag2.y);
      }

       

      drag2.addEventListener(MouseEvent.MOUSE_UP, forth);

       

      function forth(event: MouseEvent): void {
              drag2.stopDrag();

              if (drag2.hitTestObject(target2)) {
               drag2.x = target2.x;
               drag2.y = target2.y;
               drag2.buttonMode = false;
               drag2.stopDrag();

      } else {
               drag2.x = startPosition2.x;
               drag2.y = startPosition2.y;
      }
              startPosition2 = null;
      }