removal.js 783 B

1234567891011121314151617181920212223242526272829303132333435
  1. var stage = new mtm.Stage('c'),
  2. balls = [],
  3. count = 20,
  4. ball;
  5. for (var i=0; i<count; i++) {
  6. ball = new Ball();
  7. ball.x = Math.random() * stage.width;
  8. ball.y = Math.random() * stage.height;
  9. ball.vx = Math.random() * 2-1;
  10. ball.vy = Math.random() * 2-1;
  11. stage.shapes.push(ball);
  12. balls.push(ball);
  13. }
  14. stage.play(function() {
  15. for (var i=balls.length-1; i>=0; i--) {
  16. ball = balls[i];
  17. ball.x += ball.vx;
  18. ball.y += ball.vy;
  19. console.log("works");
  20. if( ball.x - ball.radius > stage.width ||
  21. ball.x + ball.radius < 0 ||
  22. ball.y - ball.radius > stage.height ||
  23. ball.y + ball.radius < 0) {
  24. stage.shapes.splice(i, 1);
  25. balls.splice(i, 1);
  26. if (balls.length <= 0 ) {
  27. stage.stop();
  28. }
  29. }
  30. }
  31. });