4 Replies Latest reply on Jan 10, 2013 11:58 AM by amarnadh_dev

    hi help me with drag and drop with as3 please

    amarnadh_dev

      hello frnds,

      i am a newbie to as3 in flash and got a prob with drag and drop functionality

       

      var dragArray:Array = [square_word, circle_word, triangle_word];

       

       

      var matchArray:Array = [squareMatch, circleMatch, triangleMatch];

       

       

      var posArray:Array = [ {x:276, y:207}, {x:443, y:207}, {x:107, y:207} ];

       

       

       

       

       

      var currentClip:MovieClip;

       

       

      var startX:Number;

       

       

      var startY:Number;

       

       

       

       

       

      for(var i:int = 0; i < dragArray.length; i++) {

       

       

          dragArray[i].buttonMode = true;

       

       

          dragArray[i].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);

       

       

          //matchArray[i].alpha = 0.2;

       

       

      }

       

       

       

       

       

      function item_onMouseDown(event:MouseEvent):void {

       

       

          currentClip = MovieClip(event.currentTarget);

       

       

          startX = currentClip.x;

       

       

          startY = currentClip.y;

       

       

          addChild(currentClip); //bring to the front

       

       

          currentClip.startDrag();

       

       

          stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);

       

       

      }

       

       

       

       

       

      function stage_onMouseUp(event:MouseEvent):void {

       

       

          stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);

       

       

          currentClip.stopDrag();

       

       

          var index:int = dragArray.indexOf(currentClip);

       

       

          var matchClip:MovieClip = MovieClip(matchArray[index]);

       

       

          if(matchClip.hitTestPoint(currentClip.x, currentClip.y, true)) {

       

       

              //a match was made! position the clip using the posArray values:

       

       

              currentClip.x = posArray[index].x;

       

       

              currentClip.y = posArray[index].y;

       

       

              //make it not draggable anymore:

       

       

              currentClip.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);

       

       

              currentClip.buttonMode = false;

       

       

          } else {

       

       

              //match was not made, so send the clip back where it started:

       

       

              currentClip.x = startX;

       

       

              currentClip.y = startY;

       

       

          }

       

      in this code i dont want the object to snap back to its original position if it is dropped in a wrong movieclip(like we should have the availability of droping the circle drag object to square alse ) and then we should have button to check to see if everything is correctly matched, if not after clicking another button "correctitself" we should get the correct objects droping on correct locations automatically

       

      please help with any suggestion ASAP

       

       

      }

        • 1. Re: hi help me with drag and drop with as3 please
          kglad Adobe Community Professional & MVP

          assign your matchall button listener to be checkAllMatchF and use:

           

           

          var dragArray:Array = [square_word, circle_word, triangle_word];

           

           

          var matchArray:Array = [squareMatch, circleMatch, triangleMatch];

           

           

          var posArray:Array = [ {x:276, y:207}, {x:443, y:207}, {x:107, y:207} ];

           

           

           

           

           

          var currentClip:MovieClip;

           

           

          var startX:Number;

           

           

          var startY:Number;

           

           

           

           

           

          for(var i:int = 0; i < dragArray.length; i++) {

           

           

              dragArray[i].buttonMode = true;

           

           

              dragArray[i].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);

           

           

              //matchArray[i].alpha = 0.2;

           

           

          }

           

           

           

           

           

          function item_onMouseDown(event:MouseEvent):void {

           

           

              currentClip = MovieClip(event.currentTarget);

           

           

              startX = currentClip.x;

           

           

              startY = currentClip.y;

           

           

              addChild(currentClip); //bring to the front

           

           

              currentClip.startDrag();

           

           

              stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);

           

           

          }

           

           

           

           

           

          function stage_onMouseUp(event:MouseEvent):void {

           

           

              stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);

           

           

              currentClip.stopDrag();

           

           

              var index:int = dragArray.indexOf(currentClip);

           

           

              var matchClip:MovieClip = MovieClip(matchArray[index]);

           

           

              if(matchClip.hitTestPoint(currentClip.x, currentClip.y, true)) {

           

           

                  //a match was made! position the clip using the posArray values:

           

           

                  currentClip.x = posArray[index].x;

           

           

                  currentClip.y = posArray[index].y;

           

           

                  //make it not draggable anymore:

           

           

                  currentClip.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);

           

           

                  currentClip.buttonMode = false;

           

           

              } else {

           

           

                  //match was not made, so send the clip back where it started:

           

           

                 // currentClip.x = startX;

           

           

                 // currentClip.y = startY;

           

           

              }

           

           

           

          }

          function checkAllMatchF():Boolean{

          for(var i:int=0;i<dragArray.length;i++){

          if(dragArray[i].x!=matchArray[i].x || dragArray[i].y!=matchArray[i].y){

          return false;

          }

          }

          return true;

          }

          • 2. Re: hi help me with drag and drop with as3 please
            amarnadh_dev Level 1

            Thank you Kglad for trying to help.

             

            i am really a beginner and not able to understand this code or where to put it.....

             

            kindly help me with that

            • 3. Re: hi help me with drag and drop with as3 please
              kglad Adobe Community Professional & MVP

              if the name of your button that checks for matches if matchCheck_btn, just copy the following code.:

               

              matchCheck_btn.addEventListener(MouseEvent.CLICK,checkAllMatchF);

               

              correctitself.addEventListener(MouseEvent.CLICK,matchAllF);

               

              function matchAllF(e:MouseEvent):void{

              for(var i:int=0;i<dragArray.length;i++){

              dragArray[i].x=matchArray[i].x;

              dragArray[i].y=matchArray[i].y

              }

               

              var dragArray:Array = [square_word, circle_word, triangle_word];

               

               

              var matchArray:Array = [squareMatch, circleMatch, triangleMatch];

               

               

              var posArray:Array = [ {x:276, y:207}, {x:443, y:207}, {x:107, y:207} ];

               

               

               

               

               

              var currentClip:MovieClip;

               

               

              var startX:Number;

               

               

              var startY:Number;

               

               

               

               

               

              for(var i:int = 0; i < dragArray.length; i++) {

               

               

                  dragArray[i].buttonMode = true;

               

               

                  dragArray[i].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);

               

               

                  //matchArray[i].alpha = 0.2;

               

               

              }

               

               

               

               

               

              function item_onMouseDown(event:MouseEvent):void {

               

               

                  currentClip = MovieClip(event.currentTarget);

               

               

                  startX = currentClip.x;

               

               

                  startY = currentClip.y;

               

               

                  addChild(currentClip); //bring to the front

               

               

                  currentClip.startDrag();

               

               

                  stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);

               

               

              }

               

               

               

               

               

              function stage_onMouseUp(event:MouseEvent):void {

               

               

                  stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);

               

               

                  currentClip.stopDrag();

               

               

                  var index:int = dragArray.indexOf(currentClip);

               

               

                  var matchClip:MovieClip = MovieClip(matchArray[index]);

               

               

                  if(matchClip.hitTestPoint(currentClip.x, currentClip.y, true)) {

               

               

                      //a match was made! position the clip using the posArray values:

               

               

                      currentClip.x = posArray[index].x;

               

               

                      currentClip.y = posArray[index].y;

               

               

                      //make it not draggable anymore:

               

               

                      currentClip.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);

               

               

                      currentClip.buttonMode = false;

               

               

                  } else {

               

               

                      //match was not made, so send the clip back where it started:

               

               

                     // currentClip.x = startX;

               

               

                     // currentClip.y = startY;

               

               

                  }

               

               

               

              }

              function checkAllMatchF(e:MouseEvent):Boolean{

              for(var i:int=0;i<dragArray.length;i++){

              if(dragArray[i].x!=matchArray[i].x || dragArray[i].y!=matchArray[i].y){

              //

              }

              }

              correctitself.visible=true;

              }

              1 person found this helpful
              • 4. Re: hi help me with drag and drop with as3 please
                amarnadh_dev Level 1

                yes,

                but the prob is i am able to drag a movieclip in one array to the correct answer in another array. otherwise it is snapping back to its original position(if i drop it else where).

                 

                what i want is  - i should drag a movieclip and can drop in any other movie clip in another array, then check for the correct answer.

                 

                for example - there are 2 array

                 

                one:array = [circle,rectangle];

                two:array = [circlematch,rectanglematch];

                 

                in these above arrays i should drag circle and drop it in reactanglematch and rectangle to circlematch. then check the answer.

                 

                now whats happening is i am able drag circle and only drop it in circlematch, if i drop it in rectanglematch , it is snapping back to its original position.

                 

                this is the code once again - please help me out with this.

                ///////////////////////////////////////////////////

                var dragArray:Array = [square_word, circle_word, triangle_word];

                 

                 

                var matchArray:Array = [squareMatch, circleMatch, triangleMatch];

                 

                 

                var posArray:Array = [ {x:276, y:207}, {x:443, y:207}, {x:107, y:207} ];

                 

                 

                 

                 

                 

                var currentClip:MovieClip;

                 

                 

                var startX:Number;

                 

                 

                var startY:Number;

                 

                 

                 

                 

                 

                for(var i:int = 0; i < dragArray.length; i++) {

                 

                 

                    dragArray[i].buttonMode = true;

                 

                 

                    dragArray[i].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);

                 

                 

                    //matchArray[i].alpha = 0.2;

                 

                 

                }

                 

                 

                 

                 

                 

                function item_onMouseDown(event:MouseEvent):void {

                 

                 

                    currentClip = MovieClip(event.currentTarget);

                 

                 

                    startX = currentClip.x;

                 

                 

                    startY = currentClip.y;

                 

                 

                    addChild(currentClip); //bring to the front

                 

                 

                    currentClip.startDrag();

                 

                 

                    stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);

                 

                 

                }

                 

                 

                 

                 

                 

                function stage_onMouseUp(event:MouseEvent):void {

                 

                 

                    stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);

                 

                 

                    currentClip.stopDrag();

                 

                 

                    var index:int = dragArray.indexOf(currentClip);

                 

                 

                    var matchClip:MovieClip = MovieClip(matchArray[index]);

                 

                 

                    if(matchClip.hitTestPoint(currentClip.x, currentClip.y, true)) {

                 

                 

                        //a match was made! position the clip using the posArray values:

                 

                 

                        currentClip.x = posArray[index].x;

                 

                 

                        currentClip.y = posArray[index].y;

                 

                 

                        //make it not draggable anymore:

                 

                 

                        currentClip.removeEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);

                 

                 

                        currentClip.buttonMode = false;

                 

                 

                    } else {

                 

                 

                        //match was not made, so send the clip back where it started:

                 

                 

                        currentClip.x = startX;

                 

                 

                        currentClip.y = startY;

                 

                 

                    }

                 

                 

                }