我是学习JavaScript的初学者,目前正在构建经典的蛇游戏。我一直将我的代码推送到git进行一些小的更改,因为那样可以更轻松地跟踪问题发生的位置。
所以现在我在本地主机上运行游戏时,在控制台上看到以下错误。
然后点击控制台上的错误,我被带到
我确定没有语法错误,因此这一定是我在这里出错的一些语义。任何帮助将不胜感激。
这是我怀疑可能是导致此问题的.js文件
snakes.js
import { getInputDirection } from "./input";
export const SNAKE_SPEED = 1; //Controls the speed of the snake;
const snakeBody = [
{ x: 10, y: 11 },
{ x: 11, y: 11 },
{ x: 12, y: 11 },
];
export function update() {
const inputDirection = getInputDirection();
for (let i = snakeBody.length - 2; i >= 0; i--) {
snakeBody[i + 1] = { ...snakeBody[i] };
}
snakeBody[0].x += inputDirection.x;
snakeBody[0].y += inputDirection.y;
}
export function draw(gameBoard) {
snakeBody.forEach((segment) => {
const snakeElement = document.createElement("div");
snakeElement.style.gridRowStart = segment.y;
snakeElement.style.gridColumnStart = segment.x;
snakeElement.classList.add("snake");
gameBoard.appendChild(snakeElement);
});
}
input.js
let inputDirection = { x: 0, y: 0 };
export function getInputDirection() {
return inputDirection;
}
答案 0 :(得分:2)
这里的错误很简单。在此处将input.js
导入snakes.js
中时出错。
更改
import { getInputDirection } from "./input";
收件人
import { getInputDirection } from "./input.js";