我正在构建一个2D pacman克隆(javascript和CSS),并且我试图移动一个幽灵/木乃伊。我写了moveGhostDown / Up / Left / Right。现在,它只下降了一步。我尝试在while循环中的moveGhost中包装if条件,但它使应用程序崩溃。我如何使其保持运动并且不立即跳跃?我也尝试过使用setTimeout,但是没有用。
function moveGhostDown() {
if ((grid[ghost.y + 1][ghost.x] !== STONE) && (grid[ghost.y + 1][ghost.x] !== KEY)) {
let last = grid[ghost.y + 1][ghost.x];
grid[ghost.y][ghost.x] = last;
ghost.y = ghost.y + 1;
grid[ghost.y][ghost.x] = MUMMY;
}
}
function moveGhost() {
if ((pacman.x > ghost.x) && (pacman.y > ghost.y)) {
moveGhostDown();
// moveGhostRight();
// moveLeft();
} else if ((pacman.x >= ghost.x) && (pacman.y <= ghost.y)) {
moveGhosrUp();
} else if ((pacman.x < ghost.x) && (pacman.y < ghost.y)) {
moveGhostRight();
// moveGhostLeft();
}
}
答案 0 :(得分:0)
moveGhosrUp
好像您有错字