2015-04-01 134 views
0

我正在关注使用画布和JavaScript在HTML中创建简单游戏的在线教程。我试图加载什么似乎是完全正常的代码,但没有什么是显示在屏幕上了,我收到在控制台说未捕获的SyntaxError:意外的令牌=在Chrome中

未捕获的SyntaxError错误:意外的记号=

这是我的代码的部分,其似乎是不正确的:

player = { 
     x: null, 
     y: null, 
     width: 20, 
     height: 100, 

     update = function() {}, 
     draw = function() { 
      ctx.fillRect(this.x, this.y, this.width, this.height) 
     }; 
    }; 

PS - 此错误显示在Chrome中。

+1

你应该向我们展示前一行 - 下一行 – axelduch 2015-04-01 08:34:27

+0

我不知道你的exac t代码,但我谷歌,发现这个http://stackoverflow.com/questions/19699257/uncaught-syntaxerror-unexpected-token-in-google-chrome – 2015-04-01 08:37:17

+0

我刚刚阅读该页面,但我仍然不知道该怎么办! – 2015-04-01 08:40:22

回答

1

您正在使用=标志的文本对象的定义,只是:

update : function() {}, 
    draw : function() { 
     ctx.fillRect(this.x, this.y, this.width, this.height); 
    } 

由于amtd在他的回答中指定取代他们,另一个错误会后draw清晰显示出来,因为你是把一个额外的;字面对象定义内的非法令牌

1

将函数设置为对象的属性时,仍然需要使用:而不是=来声明它们。

您也不需要;之后的功能,因为一旦:问题得到解决,会给你一个错误。

player = { 
    x: null, 
    y: null, 
    width: 20, 
    height: 100, 
    update : function() {}, 
    draw : function() { 
     ctx.fillRect(this.x, this.y, this.width, this.height) 
    } 
}; 
+1

还需要从绘制函数删除分号 玩家= { X:空, Y:空, 宽度:20, 高度:100, 更新:函数(){}, 平局:函数(){ ctx.fillRect(this.x,this.y,this.width,this.height) } }; – Tommyka 2015-04-01 08:40:12

相关问题