gpt4 book ai didi

Javascript/jquery速度程序代码不递增

转载 作者:行者123 更新时间:2023-11-28 01:38:03 25 4
gpt4 key购买 nike

我一生都无法弄清楚为什么每次调用 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>

最佳答案

看来您没有调用 checkKeys() - 您希望从哪里调用它?

关于Javascript/jquery速度程序代码不递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21260894/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com