6 Replies Latest reply on May 12, 2010 7:53 AM by Cyman666

    Drag and drop to multiple targets

    Cyman666

      Hi

      Im trying to create a simple drag and drop app where you can drap a movieclip into multiple areas.

      This is the code i have so far (taken from a tutorial i found) thats works perfectly for 1 target but not for multiples. I initially tried giving the targets the same instance name but that didnt work.

       

      stop();
      var startX:Number;
      var startY:Number;


      square_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
      square_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);

       


      function pickUp(event:MouseEvent):void {
      square_mc.gotoAndStop (2);
      event.target.startDrag(true);
      event.target.parent.addChild(event.target);
      startX = event.target.x;
      startY = event.target.y;
      }
      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 = "you have 5 days left";
       
        event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
        event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
        event.target.buttonMode = false;
        event.target.x = myTarget.x;
        event.target.y = myTarget.y;
         } else {
        square_mc.gotoAndStop (1);
        event.target.x = startX;
        event.target.y = startY;
      }
      if(counter == 4){
              reply_txt.text = "Congrats, you're finished!";
          }
      }

      square_mc.buttonMode = true;

       

      does anyone have any ideas?

      Many thanks

      Simon

        • 1. Re: Drag and drop to multiple targets
          kglad Adobe Community Professional & MVP

          you're checking if the drop target's parent is the same as square_mc.  that's not what you probably want.

           

          what do you want to check when your mc is dropped?

          • 2. Re: Drag and drop to multiple targets
            Cyman666 Level 1

            Well the code above works fine as it is, as in when the mc is dropped over the target it will snap to it, my problem is that i want multiple targets that the one mc will snap to

            • 3. Re: Drag and drop to multiple targets
              kglad Adobe Community Professional & MVP

              are all your targets children of square_mc?

              • 4. Re: Drag and drop to multiple targets
                Cyman666 Level 1

                not sure, how would i tell?

                • 5. Re: Drag and drop to multiple targets
                  zeroskillz Level 3

                  Here you go, I changed a couple things. First, I made the "decision" that all your drop targets would be named 'target'.

                   

                  stop();
                  var startX:Number;
                  var startY:Number;
                  
                  square_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
                  square_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
                  square_mc.buttonMode = true;
                  
                  function pickUp(event:MouseEvent):void 
                  {
                       square_mc.gotoAndStop (2);
                       event.target.startDrag()//true);
                       event.target.parent.addChild(event.target);
                       startX = event.target.x;
                       startY = event.target.y;
                  }
                  
                  function dropIt(event:MouseEvent):void 
                  {
                          // changed the bit here to set 'myTarget' after the null and name checks are made
                       event.target.stopDrag();
                  
                       if (event.target.dropTarget != null && event.target.dropTarget.parent.name == "target")
                       {
                            var myTarget = event.target.dropTarget.parent;
                            reply_txt.text = "you have 5 days left";
                         
                            event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
                            event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
                            event.target.buttonMode = false;
                            event.target.x = myTarget.x;
                            event.target.y = myTarget.y;
                            } else {
                            square_mc.gotoAndStop (1);
                            event.target.x = startX;
                            event.target.y = startY;
                       }
                       if(counter == 4)
                       {
                          reply_txt.text = "Congrats, you're finished!";
                          }
                  }
                  
                   
                  

                   

                   

                  HTH!

                  -Ted

                  • 6. Re: Drag and drop to multiple targets
                    Cyman666 Level 1

                    zeroskillz, that is spot on!!!

                    you are skillfull!

                     

                     

                    Thank you very much


                    Simon