04-ball-toss.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //***************************Scene - Update Every Frame Event********************//
  2. // get the position of the mouse pointer
  3. var mousePosition = getPointerPos();
  4. // check to see if the space bar is pressed
  5. var spaceIsPressed = isKeyPressed(Keys.space);
  6. // if the space bar is pressed
  7. // and if the ball is not already launched
  8. if( spaceIsPressed && !ball.isLaunched ) {
  9. // update the message
  10. message.text("Launched!");
  11. // give the ball x and y velocities
  12. ball.velocityX = mousePosition.x - cannon.x();
  13. ball.velocityY = mousePosition.y - cannon.y();
  14. // launch the ball
  15. ball.isLaunched = true;
  16. }
  17. //*******************ball Object - Initialize When Scene Starts Event*************//
  18. // track if the ball has been launched
  19. $this.isLaunched = false;
  20. // reset the ball's position
  21. // and set isLaunched to false
  22. $this.resetBall = function() {
  23. $this.x(35);
  24. $this.y(550);
  25. $this.isLaunched = false;
  26. };
  27. //*******************ball Object - Update Every Frame Event***********************//
  28. // if the ball is launched
  29. if ($this.isLaunched) {
  30. // move the ball based on its velocity
  31. $this.moveX($this.velocityX);
  32. $this.moveY($this.velocityY);
  33. // change the y velocity due to gravity
  34. $this.velocityY += 10;
  35. // if the ball goes off the screen
  36. if ($this.x() > 800 || $this.y() > 600) {
  37. //reset ball and update message
  38. $this.resetBall();
  39. message.text("Miss!");
  40. }
  41. }
  42. // if the ball touches the basket
  43. if($this.isTouching(basket)) {
  44. $this.resetBall()
  45. message.text("Score!")
  46. }
  47. //********************basket Group - Update Every Frame Event*********************//
  48. // move the basket group
  49. $this.moveY();
  50. // if it reaches the top
  51. if($this.y() < 100) {
  52. $this.speedY(50);
  53. }
  54. // if it reaches the bottom
  55. if($this.y() > 500) {
  56. $this.speedY(-50);
  57. }
  58. //********************cannon Object - Update Evey Frame Event*********************//
  59. // point the cannon to the cursor
  60. $this.pointToCursor();