javascript Uncaught SyntaxError: Unexpected token this Javasctript Error

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18219774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 11:06:40  来源:igfitidea点击:

Uncaught SyntaxError: Unexpected token this Javasctript Error

javascriptkeyboardsyntax-error

提问by jatmdm

Trying to create an actor class for an html5 game, but I get an Uncaught SyntaxError. Not sure at all what it means or how to fix it. Other questions like this are usually about Jquery and I couldn't find any solutions that fit my problem.

试图为 html5 游戏创建一个 actor 类,但我得到一个 Uncaught SyntaxError。完全不知道这意味着什么或如何解决它。像这样的其他问题通常是关于 Jquery 的,我找不到任何适合我的问题的解决方案。

    function Actor(x, y){
//...
        this.direction = 270;
//...
    }


    Actor.prototype.update = function(){
        if(this.speed >= this.maxspeed)
            this.speed = this.maxspeed;

            if(Key.isDown(Key.getCode("a")) this.direction -= 1; // < -- ERROR OCCURS HERE
            if(Key.isDown(Key.getCode("d")) this.direction +=1;
            if(Key.isDown(Key.getCode(" ") this.move();
     }

回答by hchau

You're missing a closing parentheses in all your if statements. They should be

您在所有 if 语句中都缺少右括号。他们应该是

if(Key.isDown(Key.getCode("a"))) this.direction -= 1;
if(Key.isDown(Key.getCode("d"))) this.direction +=1;
if(Key.isDown(Key.getCode(" "))) this.move();

回答by Alex Wayne

if(Key.isDown(Key.getCode("a")) this.direction -= 1;

You have 3 opening parens, and only 2 closing ones.

你有 3 个开括号,只有 2 个闭括号。

if(Key.isDown(Key.getCode("a"))) this.direction -= 1;
//                             ^ added

You have multiple lines with this same problem. Correct them all.

您有多条线路有同样的问题。全部纠正。