简体   繁体   English

球不平稳移动

[英]Ball not moving smoothly

I wanted to do a simple moving ball, with the arrows, so I did: 我想用箭头做一个简单的移动球,所以我做了:

<canvas id="canvas" width="300" height="300" ></canvas>

<script>
var x=120;
var y=120;
var key,pos=0;

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var img=new Image();
img.onload=function()
{
    ctx.drawImage(img,x,y);
}
img.src="http://www.infogridpacific.com/blog/i/ball_online.png";

document.onkeydown=function(e)
{
    pos=1;
    key=window.event?e.keyCode:e.which;
}
document.onkeyup=function(e){pos=0;}

setInterval(function()
{
    if(pos==0)return;
    if(key==37)x-=2;
    if(key==38)y-=2;
    if(key==39)x+=2;
    if(key==40)y+=2;
    canvas.width=canvas.width;
    ctx.drawImage(img,x,y);
},5);
</script>

http://jsfiddle.net/mageek/ny3uz/6/ http://jsfiddle.net/mageek/ny3uz/6/

But if you do left then right, the ball stops for one second then start moving in the opposite direction. 但是,如果您先左右移动,则球停了一秒钟,然后开始朝相反的方向移动。 Any idea how to fix this? 任何想法如何解决这个问题?

I suspect you are probably still holding the left arrow key when you press the right arrow key, then let go of the left arrow slightly later. 我怀疑您在按向右箭头键时可能仍会按住向左箭头键,然后稍稍松开向左箭头。

So the sequence of events goes like this: 因此,事件的顺序如下所示:

(you press left)             key=37 pos=1
(ball moves left for a bit)
(you press right)            key=39 pos=1
(you let go of left)         key=39 pos=0
(the ball stops moving)
(1s later the OS autorepeat kicks in) key=39 pos=1
(ball moves right)

You need to keep track of how many keys have been pressed/released, and/or make use of the keycode in the up event to check which key was raised. 您需要跟踪已按下/释放了多少个键,和/或在up事件中使用键代码来检查哪个键被提出。 Ideally, rather than just recording the value of the last keypress, keep track of all keys currently down. 理想情况下,不仅要记录上一次按键的值,还要跟踪当前按下的所有按键。

This version is completely smooth and also lets you move diagonally: 这个版本是完全平滑的,还可以使您沿对角线移动:

var x=120;
var y=120;
var key = [];

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var img=new Image();
img.onload=function()
{
    ctx.drawImage(img,x,y);
}
img.src="http://www.infogridpacific.com/blog/i/ball_online.png";

document.onkeydown=function(e)
{
    code=window.event?e.keyCode:e.which;
    key[code]=1;
}
document.onkeyup=function(e)
{
    code=window.event?e.keyCode:e.which;
    key[code]=0;
}

setInterval(function()
{
    if(key[37])x-=2;
    if(key[38])y-=2;
    if(key[39])x+=2;
    if(key[40])y+=2;
    canvas.width=canvas.width;
    ctx.drawImage(img,x,y);
},5);

simply count keypresses/releases. 只需计算按键/释放。 this way it won't stop if you have two keys pressed. 这样,如果您按下两个键,它不会停止。

var x=120;
var y=120;
var key,pos=0;

var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
var img=new Image();
img.onload=function(){
ctx.drawImage(img,x,y);
}
img.src="http://www.infogridpacific.com/blog/i/ball_online.png";

document.onkeydown=function(e){
pos++;
key=window.event?e.keyCode:e.which;
}
document.onkeyup=function(e){pos--}

setInterval(function(){
if(pos==0)return;
if(key==37)x-=2;
if(key==38)y-=2;
if(key==39)x+=2;
if(key==40)y+=2;
canvas.width=canvas.width;
ctx.drawImage(img,x,y);
},5);

http://jsfiddle.net/stMh9/ http://jsfiddle.net/stMh9/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM