user13903745
user13903745

Reputation:

Error shown while running Javascript game

I am a beginner in learning JavaScript and currently in the process of building a classic game of snakes. I keep pushing my code to git in small changes since that way it becomes easier to track where the issue is happening.

So now I see the below error on the console when i run my game on localhost.

ref2

and on click of the error on console, i get taken to

ref1

There is no syntax error which I am sure so it must be some semantics i am going wrong about here. Any help would be greatly appreciated.

This is my .js file I suspect might be causing the issue

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;
}

Upvotes: 1

Views: 65

Answers (1)

shuberman
shuberman

Reputation: 1490

The error here is simple. You have an error with importing your input.js in snakes.js here.

Change

import { getInputDirection } from "./input";

To

import { getInputDirection } from "./input.js";

Upvotes: 2

Related Questions