06-01-meteors-deluxe.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. Ninjas are presented with a completely blank scene and will have to build an entire
  3. activity from the ground up using everything that they’ve learned in Yellow Belt.
  4. */
  5. //*********************Scene Object - Initialize When Scene Starts Event***********//
  6. if ($this.scene.state() == "PLAY") {
  7. var nrock = rocks.clone();
  8. nrock.x(random(800));
  9. nrock.y(random(600));
  10. }
  11. //**************rock Object - Initialize When Scene Starts Event******************//
  12. if ($this.scene.state() == "PLAY") {
  13. $this.speedX(random(50,-50));
  14. $this.speedY(random(50,-50));
  15. $this.rot = random(30,-30);
  16. }
  17. //******************rock Object - Update Every Frame Event*************************//
  18. moveX($this);
  19. moveY($this);
  20. $this.spin($this.rot);
  21. // keep rocks in the scene
  22. if($this.y() > 600) {
  23. $this.y(0);
  24. }
  25. if($this.y() < 0) {
  26. $this.y(600);
  27. }
  28. if($this.x() > 800) {
  29. $this.x(0);
  30. }
  31. if($this.x() < 0) {
  32. $this.x(800);
  33. }
  34. // collision check
  35. //find the bullet on the scene
  36. var bullet = $this.scene.projectile;
  37. if(bullet && $this.isTouching(bullet)) {
  38. //instead of making the variable, we add to the score directly
  39. lblScore.text(parseInt(lblScore.text() + 10));
  40. $this.scaleX($this.scaleX()/2); //start to reduce the rock by 1/2
  41. $this.scaleY($this.scaleY()/2);
  42. $this.offsetX($this.offsetX()/2);
  43. $this.offsetY($this.offsetY()/2);
  44. if($this.scaleX() >= 0.125) {
  45. var nrock = $this.clone(); //split the rock as long as as it's bigger than 0.125
  46. } else {
  47. $this.remove();
  48. }
  49. $this.scene.projectile = null; //remove bullet and reseet value of scene projectile
  50. bullet.remove();
  51. }
  52. //*******************rocket Group - Update Every Frame Event***********************//
  53. var leftPressed = isKeyPressed(Keys.leftArrow);
  54. var rightPressed = isKeyPressed(Keys.rightArrow);
  55. var upPressed = isKeyPressed(Keys.upArrow);
  56. if(upPressed) { // if pressed, move rocket forward
  57. $this.moveForwardByRotation();
  58. }
  59. if(leftPressed) { // if pressed, rotate to the left
  60. $this.spin(-40);
  61. }
  62. if(rightPressed) {
  63. $this.spin(40);
  64. }
  65. // keep rocket in the scene
  66. if ($this.y() > 600) {
  67. $this.y(0);
  68. }
  69. if ($this.y() < 0) {
  70. $this.y(600);
  71. }
  72. if ($this.x()>800) {
  73. $this.x(0);
  74. }
  75. if( $this.x()<0) {
  76. $this.x(800);
  77. }
  78. // projectile functionality - rocket group talks to bullet, creates a 'moving' boolean
  79. //bullet is listening for moving to be true
  80. var spacePresssed = isKeyPressed(Keys.space)
  81. var bullet = $this.findName("bullet"); //find the active bullet on the scene
  82. if(spacePressed) {
  83. if(!$this.scene.projectile) {
  84. var b = bullet.clone(true, true, $this.scene); //cloning the bullet from bullet
  85. b.x(bullet.getStagePos().x); //getting the x,y and rotation in the scene
  86. b.y(bullet.getStagePos().y);
  87. b.rotation($this.rotation());
  88. b.moving = true; //set boolean to true so bullet will excecute the code when moving is true
  89. b.z(1000); //ensure it's always on top of scene
  90. $this.scene.projectile = b;
  91. }
  92. }
  93. //*****************spaceShip Object - Update Every Frame Event********************//
  94. // animation code
  95. var upPressed = isKeyPressed(Keys.upArrow);
  96. if (upPressed) {
  97. $this.animation("thrust");
  98. $this.incrementAnimation();
  99. } else {
  100. $this.animation("default");
  101. }
  102. //******************bullet Object - Update Every Frame Event**********************//
  103. //the moving boolean created from rocket group and so it knows to shoot when the
  104. //space bar is pressed
  105. if ($this.moving) {
  106. $this.moveForwardByRotation();
  107. // condition here will prevent overloading the screen with bullets,
  108. // it checks for current bullets on the screen
  109. if ($this.y() > 600 || $this.y() < 0 || $this.x() < 0 || $this.x() > 800) {
  110. $this.scene.projectile = null;
  111. $this.remove(); // rock is removed when collision occurs
  112. }
  113. }
  114. /**
  115. Ninjas are presented with a completely blank scene and will have to build an entire
  116. activity from the ground up using everything that they’ve learned in Yellow Belt.
  117. */
  118. //*********************Scene Object - Initialize When Scene Starts Event***********//
  119. if ($this.scene.state() == "PLAY") {
  120. var nrock = rocks.clone();
  121. nrock.x(random(800));
  122. nrock.y(random(600));
  123. }
  124. //**************rock Object - Initialize When Scene Starts Event******************//
  125. if ($this.scene.state() == "PLAY") {
  126. $this.speedX(random(50,-50));
  127. $this.speedY(random(50,-50));
  128. $this.rot = random(30,-30);
  129. }
  130. //******************rock Object - Update Every Frame Event*************************//
  131. moveX($this);
  132. moveY($this);
  133. $this.spin($this.rot);
  134. // keep rocks in the scene
  135. if($this.y() > 600) {
  136. $this.y(0);
  137. }
  138. if($this.y() < 0) {
  139. $this.y(600);
  140. }
  141. if($this.x() > 800) {
  142. $this.x(0);
  143. }
  144. if($this.x() < 0) {
  145. $this.x(800);
  146. }
  147. // collision check
  148. //find the bullet on the scene
  149. var bullet = $this.scene.projectile;
  150. if(bullet && $this.isTouching(bullet)) {
  151. //instead of making the variable, we add to the score directly
  152. lblScore.text(parseInt(lblScore.text() + 10));
  153. $this.scaleX($this.scaleX()/2); //start to reduce the rock by 1/2
  154. $this.scaleY($this.scaleY()/2);
  155. $this.offsetX($this.offsetX()/2);
  156. $this.offsetY($this.offsetY()/2);
  157. if($this.scaleX() >= 0.125) {
  158. var nrock = $this.clone(); //split the rock as long as as it's bigger than 0.125
  159. } else {
  160. $this.remove();
  161. }
  162. $this.scene.projectile = null; //remove bullet and reseet value of scene projectile
  163. bullet.remove();
  164. }
  165. //*******************rocket Group - Update Every Frame Event***********************//
  166. var leftPressed = isKeyPressed(Keys.leftArrow);
  167. var rightPressed = isKeyPressed(Keys.rightArrow);
  168. var upPressed = isKeyPressed(Keys.upArrow);
  169. if(upPressed) { // if pressed, move rocket forward
  170. $this.moveForwardByRotation();
  171. }
  172. if(leftPressed) { // if pressed, rotate to the left
  173. $this.spin(-40);
  174. }
  175. if(rightPressed) {
  176. $this.spin(40);
  177. }
  178. // keep rocket in the scene
  179. if ($this.y() > 600) {
  180. $this.y(0);
  181. }
  182. if ($this.y() < 0) {
  183. $this.y(600);
  184. }
  185. if ($this.x()>800) {
  186. $this.x(0);
  187. }
  188. if( $this.x()<0) {
  189. $this.x(800);
  190. }
  191. // projectile functionality - rocket group talks to bullet, creates a 'moving' boolean
  192. //bullet is listening for moving to be true
  193. var spacePresssed = isKeyPressed(Keys.space)
  194. var bullet = $this.findName("bullet"); //find the active bullet on the scene
  195. if(spacePressed) {
  196. if(!$this.scene.projectile) {
  197. var b = bullet.clone(true, true, $this.scene); //cloning the bullet from bullet
  198. b.x(bullet.getStagePos().x); //getting the x,y and rotation in the scene
  199. b.y(bullet.getStagePos().y);
  200. b.rotation($this.rotation());
  201. b.moving = true; //set boolean to true so bullet will excecute the code when moving is true
  202. b.z(1000); //ensure it's always on top of scene
  203. $this.scene.projectile = b;
  204. }
  205. }
  206. //*****************spaceShip Object - Update Every Frame Event********************//
  207. // animation code
  208. var upPressed = isKeyPressed(Keys.upArrow);
  209. if (upPressed) {
  210. $this.animation("thrust");
  211. $this.incrementAnimation();
  212. } else {
  213. $this.animation("default");
  214. }
  215. //******************bullet Object - Update Every Frame Event**********************//
  216. //the moving boolean created from rocket group and so it knows to shoot when the
  217. //space bar is pressed
  218. if ($this.moving) {
  219. $this.moveForwardByRotation();
  220. // condition here will prevent overloading the screen with bullets,
  221. // it checks for current bullets on the screen
  222. if ($this.y() > 600 || $this.y() < 0 || $this.x() < 0 || $this.x() > 800) {
  223. $this.scene.projectile = null;
  224. $this.remove(); // rock is removed when collision occurs
  225. }
  226. }