06-01-meteors-deluxe.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. }