Posted By

Winkyboy on 04/22/09


Tagged

game detection drop drag as2 collision colliding


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

yeco


AS2 Basic Drag-Drop Collision


 / Published in: ActionScript
 

URL: http://dl.dropbox.com/u/316550/code-AS2_drag-drop-collision.zip

CS4

  1. #include "mc_tween2.as"
  2. // if you don't have mc_tween, comment it out.
  3.  
  4. var startX:Number = 0; // original X position of item picked up
  5. var startY:Number = 0; // original Y position of item picked up
  6. var drawCollision:Boolean = true; // for debugging
  7.  
  8. box1_mc.onPress = function() { pickup(this, 1); };
  9. box1_mc.onRelease = function() { dropoff(this, 1, box2_mc); };
  10. //box1_mc.onRollOver = function() { rollover(this, 1); };
  11. //box1_mc.onRollOut = function() { rollout(this, 1); };
  12.  
  13. function pickup(which_mc:MovieClip, intMC:Number):Void {
  14. which_mc.swapDepths(this.getNextHighestDepth()); // bring picked-up object to foreground
  15. which_mc.startDrag();
  16. startX = which_mc._x;
  17. startY = which_mc._y;
  18. }
  19.  
  20. function dropoff(which_mc:MovieClip, intMC:Number, target_mc:MovieClip):Void {
  21. which_mc.stopDrag();
  22.  
  23. if (isColliding(target_mc, which_mc)) // try removing the second parameter here. See function for more details.
  24. {
  25. status_txt.text += "\ncollided";
  26. which_mc.slideTo(85, undefined, .5);
  27. } else {
  28. status_txt.text += "\nmissed";
  29. which_mc.slideTo(startX, startY, .5);
  30. }
  31. }
  32.  
  33. function rollover(which_mc:MovieClip, intMC:Number):Void {
  34. status_txt.text += "\nrolled over";
  35. }
  36.  
  37. function rollout(which_mc:MovieClip, intMC:Number):Void {
  38. status_txt.text += "\nrolled out";
  39. }
  40.  
  41. function isColliding(target_mc:MovieClip, which_mc:MovieClip):Boolean
  42. {
  43. // If you do not send a "which_mc" this will assume you want to check on the mouse position (which will probably be within the target when the item is dropped)
  44.  
  45. obj1_x = target_mc._x;
  46. obj1_y = target_mc._y;
  47. if (which_mc) {
  48. obj2_x = which_mc._x - Number(which_mc._width / 2);
  49. obj2_y = which_mc._y - Number(which_mc._height / 2);
  50. } else {
  51. obj2_x = this._xmouse - 1;
  52. obj2_y = this._ymouse - 1;
  53. }
  54.  
  55. obj1_x2 = target_mc._width + obj1_x;
  56. obj1_y2 = target_mc._height + obj1_y;
  57. if (which_mc) {
  58. obj2_x2 = which_mc._width + obj2_x;
  59. obj2_y2 = which_mc._height + obj2_y;
  60. } else {
  61. obj2_x2 = 2 + this._xmouse;
  62. obj2_y2 = 2 + this._ymouse;
  63. }
  64.  
  65. if (drawCollision == true)
  66. {
  67. this.createEmptyMovieClip("rectangle_mc", 10);
  68. rectangle_mc._x = obj2_x;
  69. rectangle_mc._y = obj2_y;
  70. if (which_mc) {
  71. drawRectangle(rectangle_mc, which_mc._width, which_mc._height, 0x990000, 50);
  72. } else {
  73. drawRectangle(rectangle_mc, 2, 2, 0x990000, 50);
  74. }
  75.  
  76. this.createEmptyMovieClip("rectangle2_mc", 11);
  77. rectangle2_mc._x = obj1_x;
  78. rectangle2_mc._y = obj1_y;
  79. drawRectangle(rectangle2_mc, target_mc._width, target_mc._height, 0x99FF00, 50);
  80. }
  81.  
  82. if ((obj2_x > obj1_x) && (obj2_x2 < obj1_x2) && (obj2_y > obj1_y) && (obj2_y2 < obj1_y2)) {
  83. return true;
  84. } else {
  85. return false;
  86. }
  87.  
  88. }
  89.  
  90. function drawRectangle(target_mc:MovieClip, boxWidth:Number, boxHeight:Number, fillColor:Number, fillAlpha:Number):Void {
  91. with (target_mc) {
  92. beginFill(fillColor, fillAlpha);
  93. moveTo(0, 0);
  94. lineTo(boxWidth, 0);
  95. lineTo(boxWidth, boxHeight);
  96. lineTo(0, boxHeight);
  97. lineTo(0, 0);
  98. endFill();
  99. }
  100. }

Report this snippet  

You need to login to post a comment.