首页 > 解决方案 > 未捕获的语法错误:意外的令牌 { javaScript

问题描述

import { LoadScene } from "./scenes/LoadScene.js";

在这行代码我得到这个错误:'Uncaught SyntaxError: Unexpected token {'

要将代码部署到浏览器中,我使用了 Visual Studio 代码的扩展“Live Server”。

出现导入的所有代码:

/** @type {import('./../../PHAZER/phaser-3.18.1/types/phaser')} */

import { LoadScene } from "./scenes/LoadScene.js";
import { MenuScene } from "./scenes/MenuScene.js";

let game = new Phaser.Game({
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    scene: [
       LoadScene, MenuScene
    ]
});
export const CTS = 
{
    SCENES: {
        LOAD: 'LOAD',
        MENU: 'MENU'
    }
}
import { CTS } from './../CTS.js';
import { MenuScene } from './MenuScene';

export class LoadScene extends Phaser.Scene
{
    constructor(){
        super({
            key: CTS.SCENES.LOAD
        })
    }

    preload() {
        this.load.image("logo", "./../assets/logo.png");
        this.load.image('bg', './../assets/title_bg.jpg');
        this.load.image('playBt', './../assets/play_button.png');
        this.load.image('optionsBt', './../assets/options_button.png');


        let loadingBar = this.add.graphics({
            fillStyle: {
                color: '0xffffff'
            }
        });    

        this.load.on('progress', (percent) => {
            loadingBar.fillRect(0, this.game.renderer.height/2, this.game.renderer.width * percent, 50);
            console.log(percent);
        });
    }
    create() {
        console.log('Hello from LoadScene');
        this.scene.start(CTS.SCENES.MENU, 'Hello from loadScene');
    }
}
import { CTS } from './../CTS.js';

export class MenuScene extends Phaser.Scene
{
    constructor(){
        super({
            key: CTS.SCENES.MENU
        })
    }

    init(){
        console.log('MenuScene running!');
    }

    preload(){

    }

    create() {
        this.add.image(0, 0,"bg");
    }
}

和这里的 html 代码,我在其中导入(我猜)phaser.js 这是引擎文件(我猜?到目前为止我只是按照教程进行操作)

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8" />
    <title>My Game</title>
    <style type="text/css">
        body {
            margin: 10;
        }
    </style>
</head>
<body>
<script src="./phaser.js"></script> 
<script src='main.js'></script>
</body>
</html>

当我使用 parcel 做同样的事情时,它可以工作,但我得到了一些其他奇怪的行为,当我使用 http-server 时,它只是下载它应该在我进入页面时运行的 html 文件。

我正在与 Phaser 3 合作

标签: javascripthtml

解决方案


推荐阅读