bouncing.js 766 B

1234567891011121314151617181920212223242526272829
  1. var stage = new mtm.Stage('c'),
  2. ball = new Ball(stage.width/2, stage.height/2),
  3. left = 0, right = stage.width, top_ = 0, bottom = stage.height;
  4. ball.vx = Math.random() * 10 - 5;
  5. ball.vy = Math.random() * 10 - 5;
  6. ball.bounce = -0.7;
  7. stage.shapes.push(ball);
  8. stage.play(function() {
  9. ball.x += ball.vx;
  10. ball.y += ball.vy;
  11. if (ball.x + ball.radius > right){
  12. ball.x = right - ball.radius;
  13. ball.vx *= ball.bounce;
  14. } else if (ball.x - ball.radius < left) {
  15. ball.x = left + ball.radius;
  16. ball.vx *= ball.bounce;
  17. } else if (ball.y + ball.radius > bottom){
  18. ball.y = bottom - ball.radius;
  19. ball.vy *= ball.bounce;
  20. } else if (ball.y - ball.radius < top_) {
  21. ball.y = top_ + ball.radius;
  22. ball.vy *= ball.bounce;
  23. }
  24. });