acceleration2.js 430 B

123456789101112131415161718192021222324
  1. var stage = new mtm.Stage('c'),
  2. ball = new Ball(stage.width/2, stage.height/2),
  3. vx = 0,
  4. ax = 0;
  5. stage.shapes.push(ball);
  6. stage.play(function() {
  7. vx += ax;
  8. ball.x += vx;
  9. });
  10. document.addEventListener('keydown', function(e) {
  11. if (e.keyCode == 37) { //left
  12. ax = -0.2;
  13. } else if (e.keyCode == 39) { //right
  14. ax = 0.2;
  15. }
  16. }, false);
  17. document.addEventListener('keyup', function(e) {
  18. ax = 0;
  19. }, false);