01-whack-a-ninja.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //*********************Scene - Initialize When Scene Starts Event******************//
  2. if ($this.scene.state() == "PLAY") {
  3. $this.peekSpeed = 100;
  4. $this.peekDistance = 250;
  5. $this.points = 0;
  6. createTimer(2000, $this.activateNinja);
  7. }
  8. $this.activateNinja = function() {
  9. //pick and find a random ninja
  10. var randomNumber = random(1,5);
  11. var chosenNinja = $this.findName("ninja" + randomNumber);
  12. //activate the chosen ninja and move it up
  13. chosenNinja.active = true;
  14. chosenNinja.speedY(-$this.peekSpeed);
  15. };
  16. //*******************ninja Object - Initialize When Scene Starts Event**************//
  17. $this.startingY = $this.y();
  18. $this.stoppingY = $this.startingY - $this.scene.peekDistance;
  19. //********************ninja Objects - Update Every Frame Event**********************//
  20. if($this.active) { // important that the closing brace is at the very end, otherwise,
  21. //we'll lose score even when ninja is not active
  22. //move ninja up if it's active
  23. moveY($this);
  24. //if ninja peeks all the way out, start going back down
  25. if($this.y() <= $this.stoppingY) {
  26. $this.speedY($this.scene.peekSpeed);
  27. }
  28. // stop ninja if it reaches its hiding spot
  29. // subtract a point, deactivate ninja
  30. if ($this.y() >= $this.startingY) {
  31. $this.speedY(0);
  32. $this.scene.points -= 1;
  33. score.text($this.scene.points);
  34. $this.active = false;
  35. }
  36. }
  37. //***************************ninja Object - Mouse Click Event***********************//
  38. //if ninja is active
  39. if ($this.active) {
  40. //deactivate
  41. $this.active = false;
  42. //move to starting location
  43. $this.y($this.startingY);
  44. //increase and update score
  45. $this.scene.points += 1;
  46. score.text($this.scene.points);
  47. }