1 Reply Latest reply on Feb 16, 2017 4:58 AM by kglad

    Hit test for drag & drop objects

    VamshiKrishna

      Hi All,

      I am trying create a drag and drop functionality by using following code, is there any other way to check the hit test functionality.

       

      dragger.on("pressmove" || "touchstart", function(evt)

      {

        console.log("move  "+evt.currentTarget+"   ___  "+destination);

        evt.currentTarget.x = evt.stageX;

        evt.currentTarget.y = evt.stageY;

        stage.update();

        if(intersect(evt.currentTarget, destination)){

        evt.currentTarget.alpha = 0.2;

        box.graphics.clear();

        box.graphics.setStrokeStyle(3).beginStroke("0066A4").rect(0,0,destHeight,destWidth);

        }else{

        evt.currentTarget.alpha = 1;

        box.graphics.clear();

        box.graphics.setStrokeStyle(2).beginStroke("black").rect(0,0,destHeight,destWidth);

        }

      });

       

      dragger.on("pressup", function(evt) {

        if(intersect(evt.currentTarget, destination)){

          dragger.x = destination.x + destWidth/2;

          dragger.y = destination.y + destHeight/2;

          dragger.alpha = 1;

          box.graphics.clear();    

          box.graphics.setStrokeStyle(2).beginStroke("black").rect(0, 0, destHeight, destWidth);

          stage.update(evt);

        }

        else{

        dragger.x = dragger.y = 200;

        }

      });

       

      function intersect(obj1, obj2){

        var objBounds1 = obj1.getBounds().clone();

        var objBounds2 = obj2.getBounds().clone();

        var pt = obj1.globalToLocal(objBounds2.x, objBounds2.y); 

        var h1 = -(objBounds1.height / 2 + objBounds2.height);

        var h2 = objBounds2.width / 2;

        var w1 = -(objBounds1.width / 2 + objBounds2.width);

        var w2 = objBounds2.width / 2;

       

        if(pt.x > w2 || pt.x < w1) return false;

        if(pt.y > h2 || pt.y < h1) return false;

       

        return true;

      }