1
我不能爲我的生活找出爲什麼我的ball.x在每次調用動畫函數時都不會增加0.2?我做了一個工作正常,只是自動增加,但我希望這個增加,當你按左或右。有任何想法嗎??Javascript/jquery速度程序代碼不會增加
<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>
你必須在checkKeys函數中使用+ =和 - =,而不僅僅是「=」! – rikpg
@rikpg編號引用BenParsons:這不相關,因爲他在繪圖函數中使用ax來影響vx。 – Stijn