03-07-PY-robot-builder.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. Decompose Robot Builder
  3. * Make robot parts draggable
  4. * Robot parts snap in correct spots
  5. * Robot parts bounce back when incorrect
  6. * findDistance() function will require teaching
  7. * Review of .x() and .y() and how they work
  8. */
  9. //All parts require $this.draggable(true); at Initialize When Scene Starts
  10. //*************body Object - Mouse Button Up Event********************//
  11. //find distance between body part and bodySpace
  12. //store it in variable called distance
  13. var distance = $this.findDistance(bodySpace);
  14. if (distance < 20) { //if it's dragged close enough to bodySpace
  15. //snap it to x and y of bodySpace
  16. $this.x(bodySpace.x());
  17. $this.y(bodySpace.y());
  18. //make it no longer draggable
  19. $this.draggable(false); //Ninja Supplies
  20. } else { //was dragged to the wrong spot
  21. //snap it back to where it started
  22. //Hungry Ninja code
  23. $this.x(616);
  24. $this.y(42);
  25. }
  26. //***********arm1 Object - Mouse Button Up Event**********************//
  27. //find distance between arm1 part and arm1Space
  28. //store it in variable called distance
  29. var distance = $this.findDistance(arm1Space);
  30. if (distance < 20) { //if it's dragged close enough to arm1Space
  31. //snap it to x and y of arm1Space
  32. $this.x(arm1Space.x());
  33. $this.y(arm1Space.y());
  34. //make it no longer draggable
  35. $this.draggable(false); //Ninja Supplies
  36. } else { //was dragged to the wrong spot
  37. //snap it back to where it started
  38. //Hungry Ninja code
  39. $this.x(634);
  40. $this.y(244);
  41. }
  42. //*************arm2 Object - Mouse Button Up Event********************//
  43. //find distance between arm2 part and arm2Space
  44. //store it in variable called distance
  45. var distance = $this.findDistance(arm2Space);
  46. if (distance < 20) { //if it's dragged close enough to arm2Space
  47. //snap it to x and y of arm2Space
  48. $this.x(arm2Space.x());
  49. $this.y(arm2Space.y());
  50. //make it no longer draggable
  51. $this.draggable(false); //Ninja Supplies
  52. } else { //was dragged to the wrong spot
  53. //snap it back to where it started
  54. //Hungry Ninja code
  55. $this.x(44);
  56. $this.y(242);
  57. }
  58. //*************head Object - Mouse Button Up Event********************//
  59. //find distance between head part and headSpace
  60. //store it in variable called distance
  61. var distance = $this.findDistance(headSpace);
  62. if (distance < 20) { //if it's dragged close enough to headSpace
  63. //snap it to x and y of headSpace
  64. $this.x(headSpace.x());
  65. $this.y(headSpace.y());
  66. //make it no longer draggable
  67. $this.draggable(false); //Ninja Supplies
  68. } else { //was dragged to the wrong spot
  69. //snap it back to where it started
  70. //Hungry Ninja code
  71. $this.x(512);
  72. $this.y(408);
  73. }
  74. //*************leg1 Object - Mouse Button Up Event**********************//
  75. //find distance between leg1 part and leg1Space
  76. //store it in variable called distance
  77. var distance = $this.findDistance(leg1Space);
  78. if (distance < 20) { //if it's dragged close enough to leg1Space
  79. //snap it to x and y of leg1Space
  80. $this.x(leg1Space.x());
  81. $this.y(leg1Space.y());
  82. //make it no longer draggable
  83. $this.draggable(false); //Ninja Supplies
  84. } else { //was dragged to the wrong spot
  85. //snap it back to where it started
  86. //Hungry Ninja code
  87. $this.x(58);
  88. $this.y(66);
  89. }
  90. //****************leg2 Object - Mouse Button Up Event*******************//
  91. //find distance between leg2 part and leg2Space
  92. //store it in variable called distance
  93. var distance = $this.findDistance(leg2Space);
  94. if (distance < 20) { //if it's dragged close enough to leg2Space
  95. //snap it to x and y of leg2Space
  96. $this.x(leg2Space.x());
  97. $this.y(leg2Space.y());
  98. //make it no longer draggable
  99. $this.draggable(false); //Ninja Supplies
  100. } else { //was dragged to the wrong spot
  101. //snap it back to where it started
  102. //Hungry Ninja code
  103. $this.x(150);
  104. $this.y(476);
  105. }
  106. //************Scene Object - Initialize When Scene Starts***************//
  107. //Make a variable to keep track of how many parts were snapped in correctly
  108. //Also Add to Scene a label called message to display text
  109. $this.correct = 0;
  110. message.text("Welcome to Robot Building v.1.0");
  111. //***********Scene Object - Update Every Frame************************//
  112. //We will check if correct = 6 every frame
  113. //When it's 6, display congratulatory message
  114. if($this.correct == 6) {
  115. message.text("Nice work, you finished!")
  116. }