javascript - 我在跳跃脚本中做错了什么?
问题描述
我正在构建一个 2D 平台游戏。而且我还没有编写跳跃和所有这些的脚本,但是我有基本的脚本可以向各个方向移动:
document.onkeydown = checkKey;
function checkKey(e) {
e = e || window.event;
if (e.keyCode == '38') {
if (yVel <= 4){
yVel += speed;
}else{
yVel = yVel
}
y -= yVel;
}
else if (e.keyCode == '40') {
if (yVel <= 4){
yVel += speed;
}else{
yVel = yVel
}
y += yVel;
}
else if (e.keyCode == '37') {
if (xVel <= 4){
xVel += speed;
}else{
xVel = xVel
}
x -= xVel;
}
else if (e.keyCode == '39') {
if (xVel <= 4){
xVel += speed;
}else{
xVel = xVel
}
x += xVel;
}
结果应该是脚本,如果需要,它可以让你对角移动,但我得到的是当你按下向上键时,例如,它会上升,但是当你按下任何其他按钮时,例如,右,然后它停止向上运动,只向右走。有项目代码:https ://repl.it/@MarkelL/Break-It 这是项目本身:https ://break-it--markell.repl.co/
解释将非常感激。谢谢!
解决方案
推荐阅读
- c# - 在 .NET 4.7.2 类库中使用 EF Core 3.1 SQLite 数据库
- ubuntu - Ubuntu ffmpeg 无法使用 libx264
- c# - c# asp.net mvc - 简化
- angular - 如何在其他组件中使用 sessionStorage.clear()?
- json - Yii2:如何从后端响应?
- jenkins - openshift jenkins中的交换内存问题使其变慢
- javascript - 以编程方式更改本机应用程序显示名称
- php - Symfony find user by role (JSON array Doctrine property)
- python - 使用 python 对数据框 pandas 进行分组
- json - jq 不区分大小写键过滤器