09-flying-ninja.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. This activity uses conditionals to creat movemtn code with the mouse. Timer and random chances are used to call functions that
  3. create obstacles. The player must avoid the obstacles for as long as possible
  4. */
  5. //********************************Scene Object - Initialize When Scene Starts Event**********************//
  6. if ($this.scene.state() == "PLAY") {
  7. // set up the score variable
  8. $this.score = 0;
  9. // clone background image
  10. var farBackgroundClone = farBackground.clone();
  11. // place the cloned background image
  12. farBackgroundClone.y(0);
  13. farBackgroundClone.x(1000);
  14. // clone the background image
  15. var closeBackgroundClone = closeBackground.clone();
  16. // place the cloned background image
  17. closeBackgroundClone.y(0);
  18. closeBackgroundClone.x(1000);
  19. // every 2 seconds, spawn a new obstacle
  20. createTimer(2000, function() {
  21. var chance = random(100);
  22. if (chance <= 25) {
  23. $this.generateBottomObstacle();
  24. } else if(chance <= 50) {
  25. $this.generateTopObstacle();
  26. } else {
  27. $this.generateBottomObstacle();
  28. $this.generateTopObstacle();
  29. }
  30. // increase score after every spawn
  31. $this.score += 10;
  32. scoreLabel.text($this.score);
  33. });
  34. }
  35. $this.generateBottomObstacle = function() {
  36. // clone the mite object
  37. var miteClone = mite.clone();
  38. // position it with random y
  39. miteClone.x(850);
  40. miteClone.y(random(350, 500));
  41. // activate the clone for collision
  42. miteClone.active = true;
  43. };
  44. $this.generateTopObstacle = function() {
  45. // clone the mite object
  46. var miteClone = mite.clone();
  47. // rotate it to flip its direction
  48. miteClone.rotation(180);
  49. // position it with random y
  50. miteClone.x(850);
  51. miteClone.y(random(100, 300));
  52. // activate the clone for collision
  53. miteClone.active = true;
  54. }
  55. //*************************farBackground Object - Update Every Frame Event*******************************//
  56. // always move the background
  57. $this.moveX();
  58. // move the image when it goes completely off the screen
  59. if ($this.x() <= -1000) {
  60. $this.x(1000);
  61. }
  62. //****************************closeBackground Object - Update Every Frame Event*****************************//
  63. // always move the background
  64. $this.moveX(); // provide more immersive feeling with parallax motion
  65. /** objects close by seem to move faster than objects far away
  66. closeBackground has faster speed than farBackground so when they
  67. both move, it will give a sense of speed
  68. */
  69. if ($this.x() <= -1000) {
  70. $this.x(1000);
  71. }
  72. //****************************mite Object - Update Every Frame Event**************************************//
  73. // always move the mite object
  74. $this.moveX();
  75. // if mite goes off screen, remove it
  76. if ($this.x() < -50) {
  77. $this.remove();
  78. }
  79. // if the mite object is active and it's touching hotSpot
  80. if ($this.active && $this.isTouching(hotSpot)) {
  81. //deactivate this mite object
  82. $this.active = false;
  83. // subtract points and update scoreLabel
  84. $this.scene.score -= 5;
  85. scoreLabel.text($this.scene.score);
  86. }
  87. //****************************ninja Group - Update Every Frame Event**************************************//
  88. // calculate how far away the mouse is from the ninja
  89. var distanceToMouse = getMouseY() - $this.y();
  90. hotSpot.z(0); // hide hotSpot behind ninja sprite
  91. // if the mouse is above the ninja
  92. // distanceToMouse will be negative
  93. if (distanceToMouse < -5) {
  94. // move ninja up
  95. $this.moveY(-75);
  96. }
  97. if (distanceToMouse > 5) {
  98. // move ninja down
  99. $this.moveY(75);
  100. }
  101. // adjust the rotation of the ninja to tilt up and down
  102. $this.rotation(distanceToMouse * 0.1);