03-ninja-race.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //***********************Scene Object - Initialize When Scene Starts Event****************//
  2. if($this. scene. state () == "PLAY" ) {
  3. // place both the road images
  4. road1.x(0);
  5. roadl.y(0);
  6. road2.x(0);
  7. road2.y(-1200);
  8. }
  9. //**********************road Object - Update Every Frame Event***************************//
  10. // move the road down
  11. $this.moveY();
  12. // check to see if the road is off the screen
  13. if ($this.y() > 1200) {
  14. // move it back to the top of the screen
  15. $this.y(-1200);
  16. }
  17. //**********************cycleRed Object - Update Every Frame Event***********************//
  18. // move the cycle left
  19. if (isKeyPressed(Keys.a)) {
  20. $this.moveX(-100);
  21. }
  22. // move the cycle right
  23. if (isKeyPressed(Keys.d)) {
  24. $this.moveX(l00);
  25. }
  26. // if the cycle touches the boost
  27. // move the cycle up
  28. if ($this.isTouching(boost)) {
  29. $this.moveY (-50);
  30. }
  31. // if the cycle touches the mud
  32. // move the cycle down
  33. if ($this.isTouching(mud)) {
  34. $this.moveY(50);
  35. }
  36. //************************cycleBlue Object - Update Every Frame Event*********************//
  37. // move the cycle left
  38. if (isKeyPressed(Keys.leftArrow)) {
  39. $this.moveX(-100);
  40. }
  41. // move the cycle right
  42. if (isKeyPressed (Keys.rightArrow)) {
  43. $this.moveX(l00);
  44. }
  45. // if the cycle touches the boost
  46. // move the cycle up
  47. if ($this.isTouching (boost)) {
  48. $this.moveY(-50);
  49. }
  50. // if the cycle touches the mud
  51. // move the cycle down
  52. if ($this.isTouching (mud)) {
  53. $this.moveY(50);
  54. }
  55. //**************************mud and boost Objects - Update Every Frame Event***************//
  56. // move it down
  57. $this.moveY();
  58. // if the it moves off the bottom of the screen
  59. if ($this.y() >= 600) {
  60. // pick a random x within the screen
  61. $this.x(random(l00, 700));
  62. // pick a random y above the screen
  63. $this.y(random(-200 , -600));
  64. }
  65. //**************************message Object - Update Every Frame Event***********************//
  66. // store they values
  67. var redY = cycleRed.y();
  68. var blueY = cycleBlue.y();
  69. // determine a winner or who is in the lead
  70. if (redY <= 50) {
  71. message.text("Red wins!");
  72. } else if (blueY <= 50) {
  73. message.text("Blue wins!");
  74. } else if (redY < blueY) {
  75. message.text("Red is in the lead!");
  76. } else if (blueY < redY) {
  77. message.text("Blue is in the lead!");
  78. } else {
  79. message.text("Tied!");
  80. }