AddThis Social Bookmark Button
Tutorial part 2 : Advanced tips and tricks
Let's implement the hitTest method with the following code :

onMouseMove = function(){
updateAfterEvent();
}

redCircle.onPress = function(){
startDrag(this,true,100,75,450,325)
}

redCircle.onRelease = function(){
this.stopDrag();
checkTarget(this);
}

redCircle.onReleaseOutside = function(){
this.stopDrag();
checkTarget(this);
}


function checkTarget(drag){

// _dropTarget method (commented so non active)
// trace(drag+ "has been dropped on "+eval(drag._droptarget));

if (drag.hitTest(greyCircle)) {
trace(drag+" has been dropped on greyCircle");
}
else {
trace("you missed the target");
}


}




You will notice that the hitTest method is more accurate than the _droptarget method but it is not perfect because it uses the bounding boxes to detect a collision and it is not a pixel collision detection.

The is more complex and perfect way to detect collusion between two shapes. the BitmapData class has a hitTest method but it is more complex and beyond the scope of that tutorial.

With what you have learned here you should be able to create some basic drag and drop activities without problem and even more.

If you still find it hard to develop more complex drag and drop activities or if you are looking for a powerful yet simple way of building drag and drop activities have a look at our Flashvalley dragNdrop and Flashvalley dragNdraw components.


Good luck :)


AddThis Social Bookmark Button
If you think this page is providing useful information, don't hesitate to leave a comment.
flashvalley
 
Copyright ©2006-2008 flashvalley.com - All rights reserved