05-07-py-bubbles.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. Like the Rain Catcher games, this game uses a timer to release the bubbles so they
  3. can float to the top. We’ll be using $this.scene.state() == “PLAY” so that the bubbles
  4. do not continue to rise after the game has stopped. The bubble used in this game may
  5. be hard to initially locate since it starts at the bottom of the screen. Use
  6. GAME OBJECTS to select it if you’re having trouble.
  7. Decomposition:
  8. * variable to keep track of score
  9. * bubbles are cloned at random x
  10. * bubbles move up
  11. * get a point when bubbles are clicked
  12. * lose a point when bubble moves off screen
  13. */
  14. //*****************Scene Object - Initialize When Scene Starts*********************//
  15. // make variable for score
  16. $this.totalScore = 0;
  17. if($this.scene.state() == "PLAY") {
  18. createTimer(1000, function() {
  19. var nbubble = bubble.clone();
  20. nbubble.x(random(720,80));
  21. nbubble.y(600);
  22. });
  23. }
  24. //*******************bubble Object - Update Every Frame Event**********************//
  25. if($this.y() > 0) { // move as long as it's on the screen
  26. $this.moveY(-100);
  27. } else { // when it is off screen
  28. $this.remove()
  29. $this.scene.totalScore -= 1;
  30. score.text($this.scene.totalScore)
  31. }
  32. //*********************bubble Object - Mouse Click Event***************************//
  33. $this.remove();
  34. $this.scene.totalScore += 1;
  35. score.text($this.scene.totalScore); // update score