Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. var ball;
  2. var ball2;
  3. var dx = 4;
  4. var dy = 4;
  5.  
  6. function start(){
  7. ball = new Circle(20);
  8. ball.setPosition(100, 100);
  9. add(ball);
  10.  
  11. setTimer(draw, 20);
  12.  
  13. ball2 = new Circle(30);
  14. ball2.setPosition(105, 105);
  15. add(ball2);
  16.  
  17. setTimer(draw, 20);
  18. }
  19.  
  20. function draw(){
  21. checkWalls();
  22. ball.move(dx, dy);
  23. }
  24.  
  25. function draw(){
  26. checkWalls();
  27. ball2.move(dx, dy);
  28. }
  29.  
  30. function checkWalls(){
  31. // Bounce off right wall
  32. if(ball.getX() + ball.getRadius() > getWidth()){
  33. dx = -dx;
  34. }
  35.  
  36. // Bounce off left wall
  37. if(ball.getX() - ball.getRadius() < 0){
  38. dx = -dx;
  39. }
  40.  
  41. // Bounce off bottom wall
  42. if(ball.getY() + ball.getRadius() > getHeight()){
  43. dy = -dy;
  44. }
  45.  
  46. // Bounce off top wall
  47. if(ball.getY() - ball.getRadius() < 0){
  48. dy = -dy;
  49. }
  50. function checkWalls(){
  51. // Bounce off right wall
  52. if(ball2.getX() + ball2.getRadius() > getWidth()){
  53. dx = -dx;
  54. }
  55.  
  56. // Bounce off left wall
  57. if(ball2.getX() - ball2.getRadius() < 0){
  58. dx = -dx;
  59. }
  60.  
  61. // Bounce off bottom wall
  62. if(ball2.getY() + ball2.getRadius() > getHeight()){
  63. dy = -dy;
  64. }
  65.  
  66. // Bounce off top wall
  67. if(ball2.getY() - ball2.getRadius() < 0){
  68. dy = -dy;
  69. }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement