我正在构建一个简单的障碍游戏,我现在遇到的问题是我可以使用箭头键左右移动,但不能上下移动。我不明白为什么当上下具有与左右相同的逻辑时它们不起作用。我对自己开发东西还很陌生,所以如果很明显的话,请放轻松。:)
document.addEventListener("DOMContentLoaded", () => {
// Grab the elements from the HTML
const floppy = document.getElementById("floppyDisk");
const gameBoard = document.querySelector(".gameBoard");
let userScore = document.getElementById("userScore");
let highscore = document.getElementById("highscore");
// Set up variables to be used later
let floppyPosX = 0;
let floppyPosY = 0;
let left = 20;
let top = 190;
// Use the arrows to move the floppy disk
function moveFloppy(e) {
if(e.keyCode == 39) {
left += 2;
floppy.style.left = floppyPosX + left + "px";
}
if(e.keyCode == 37) {
left -= 2;
floppy.style.left = floppyPosX + left + "px";
}
if(e.keycode == 38) {
top += 2;
floppy.style.top = floppyPosY + top + "px";
}
if(e.keycode == 40) {
top -= 2;
floppy.style.top = floppyPosY + top + "px";
}
}
// Invoke the moveFloppy function
document.onkeydown = moveFloppy;
// Generate Obstacles
// Function to move the obstacles
// Set function to repeat
// Call the function to generate the obstacles
})
墨色风雨
相关分类