Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Hit test for drag & drop objects

New Here ,
Feb 16, 2017 Feb 16, 2017

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;

}

814
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 16, 2017 Feb 16, 2017

createjs has a hitTest method: EaselJS v0.8.2 API Documentation : MovieClip

Translate
Community Expert ,
Feb 16, 2017 Feb 16, 2017
LATEST

createjs has a hitTest method: EaselJS v0.8.2 API Documentation : MovieClip

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines