【发布时间】:2019-04-17 20:12:17
【问题描述】:
我正在开发一个破砖游戏,虽然我的代码可以在 CodeSandbox 上运行,但当我在本地计算机上尝试它时,导入功能不起作用,游戏也随之不起作用。
我尝试移动文件夹,研究了如何移动,并尝试在类名周围添加 {},但无济于事。
这是我的 index.html 脚本链接
<script src="src/index.js"></script>
这是在我的 index.js 文件中(在文件夹“src”中)
import Game from "/src/game.js";
这就是我的 game.js 文件中的“游戏”(也在文件夹“src”中)。
export default class Game {
constructor(gameWidth, gameHeight) {
this.gameWidth = gameWidth;
this.gameHeight = gameHeight;
this.gamestate = GAMESTATE.MENU;
this.paddle = new Paddle(this);
this.ball = new Ball(this);
this.gameObjects = [];
new InputHandler(this.paddle, this);
}
在 CodeSandbox 中,游戏没有问题,因为它运行正常,而当我在本地运行它时,控制台显示“SyntaxError: Unexpected identifier 'Game'。import call 只需要一个参数。”画布是空白的。我已经被困了一段时间,真的需要一些帮助。
【问题讨论】:
-
尝试用括号括起来类声明:
export default (class Game { ... }) -
游戏路径是否正确?应该是
"./game.js";
标签: javascript import html5-canvas export