Javascript 如何在画布游戏中流畅地移动玩家?

Javascript 如何在画布游戏中流畅地移动玩家?,javascript,canvas,move,smoothing,Javascript,Canvas,Move,Smoothing,如何在画布2d游戏中流畅地移动玩家?这是我的第一个画布游戏应用程序的代码 //Canvas const canvas = document.getElementById("canvas"); canvas.style.display = "block"; canvas.height = innerHeight; canvas.width = innerWidth; canvas.style.backgroundColor = "grey"

如何在画布2d游戏中流畅地移动玩家?这是我的第一个画布游戏应用程序的代码

//Canvas
const canvas = document.getElementById("canvas");
canvas.style.display = "block";
canvas.height = innerHeight;
canvas.width = innerWidth;
canvas.style.backgroundColor = "grey";
//Context
const ctx = canvas.getContext("2d");
//Player
const player = {x: 10, y: 10, w: 20, h: 20};
//Start-position
ctx.fillRect(player.x, player.y, player.w, player.h);
//No-smooth-movement
window.onkeydown = move = (e) => {
    let key = e.keyCode;
    if     (key === 68 && player.x <= canvas.width-25) {player.x += 10;} //right
    else if(key === 65 && player.x >= 10) {player.x -= 10;} //left
    else if(key === 83 && player.y <= canvas.height-25) {player.y += 10;} //down
    else if(key === 87 && player.y >= 10) {player.y -= 10;} //up

    ctx.clearRect(0,0, canvas.width, canvas.height);
    ctx.fillRect(player.x, player.y, player.w, player.h);
}
//画布
const canvas=document.getElementById(“canvas”);
canvas.style.display=“块”;
canvas.height=内部高度;
canvas.width=内部宽度;
canvas.style.backgroundColor=“灰色”;
//上下文
const ctx=canvas.getContext(“2d”);
//玩家
constplayer={x:10,y:10,w:20,h:20};
//起始位置
ctx.fillRect(player.x,player.y,player.w,player.h);
//没有平稳的运动
window.onkeydown=move=(e)=>{
设key=e.keyCode;
如果(key==68&&player.x=10){player.x-=10;}//左
如果(key==83&&player.y=10){player.y-=10;}//向上
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.fillRect(player.x,player.y,player.w,player.h);
}

要制作更平滑的动画,需要更高的帧速率。在给定的代码中,您只需使用onkeydown事件,它不是每秒一定数量的帧。 您可以在补码或window.requestAnimationFrame中使用setInterval:

//画布
mycan.style.display=“block”;
mycan.height=内部高度;
mycan.width=内部宽度;
mycan.style.backgroundColor=“灰色”;
//上下文
const ctx=mycan.getContext(“2d”);
//玩家
constplayer={x:10,y:10,w:20,h:20};
//起始位置
ctx.fillRect(player.x,player.y,player.w,player.h);
//没有平稳的运动
window.onkeydown=move=(e)=>{
设key=e.keyCode;
如果(key==68&&player.x=10){player.x-=10;}//左
如果(key==83&&player.y=10){player.y-=10;}//向上
}
常量绘图=()=>{
ctx.clearRect(0,0,mycan.width,mycan.height);
ctx.fillRect(player.x,player.y,player.w,player.h);
};
设置间隔(()=>{
draw();
},1000/60);

要制作更平滑的动画,需要更高的帧速率。在给定的代码中,您只需使用onkeydown事件,它不是每秒一定数量的帧。 您可以在补码或window.requestAnimationFrame中使用setInterval:

//画布
mycan.style.display=“block”;
mycan.height=内部高度;
mycan.width=内部宽度;
mycan.style.backgroundColor=“灰色”;
//上下文
const ctx=mycan.getContext(“2d”);
//玩家
constplayer={x:10,y:10,w:20,h:20};
//起始位置
ctx.fillRect(player.x,player.y,player.w,player.h);
//没有平稳的运动
window.onkeydown=move=(e)=>{
设key=e.keyCode;
如果(key==68&&player.x=10){player.x-=10;}//左
如果(key==83&&player.y=10){player.y-=10;}//向上
}
常量绘图=()=>{
ctx.clearRect(0,0,mycan.width,mycan.height);
ctx.fillRect(player.x,player.y,player.w,player.h);
};
设置间隔(()=>{
draw();
},1000/60);

使用游戏引擎:使用游戏引擎:非常感谢:)执行平滑移动,但在首次单击后会有一些延迟这是按键事件的延迟,需要时间来检测重复。因此,与10或5这样的值相比,yoy可以更好地检测角色移动是真是假,这可以解决问题。我用这种方式更新了我的代码非常感谢:)实现了平滑的移动,但是在第一次单击之后会有一些延迟,这是对keydown事件的延迟,需要时间来检测重复。因此,与10或5这样的值相比,yoy可以更好地检测角色移动是真是假,这可以解决问题。我已经这样更新了我的代码