Javascript / jquery速度程序代码不会增加

时间:2014-01-21 14:28:10

标签: javascript jquery

我不能为我的生活解决为什么每次调用我的animate函数时我的ball.x都不会增加0.2?我做了一个工作正常,只是自动递增,但我希望这个按左或右按下时增加。有任何想法吗??

<script>
var canvas;
var context;
var canvasWidth;
var canvasHeight;
var ball = new Ball();
ball.radius = 15;
var vx = 0;
var ax = 0;
var keyPressed;

$(document).ready(function() {
    canvas = $("#myCanvas");
    context = canvas.get(0).getContext("2d");
    canvasWidth = canvas.width();
    canvasHeight = canvas.height();     
    animate();
});

function checkKeys(e) {
    keyPressed = e.keyCode;
    console.log(keyPressed);    
    if(keyPressed == 37)
    {
        ax = -0.2;
    }
    if(keyPressed == 39)
    {
        ax = 0.2;
    }       
}



function animate() {
    window.requestAnimationFrame(animate, canvas);
    draw();
    //console.log(vx);  
};



function draw() {
    context.clearRect(0,0,canvasWidth, canvasHeight);
    ball.y = canvasHeight / 2;
    ball.x = canvasWidth / 2;
    vx = vx + ax;
    ball.x = ball.x + vx;
    console.log(ball.x);    
    ball.draw(context);                             
}


    </script>
</head>  

  

1 个答案:

答案 0 :(得分:1)

您似乎没有打电话给checkKeys() - 您希望从哪里调用?