gpt4 book ai didi

javascript - 语法错误: invalid label with javascript game

转载 作者:行者123 更新时间:2023-11-28 15:42:49 25 4
gpt4 key购买 nike

我正在遵循以下有关如何制作太空入侵者游戏的教程 - http://www.html5rocks.com/en/tutorials/canvas/notearsgame/

但是,执行此代码时会显示标题中的错误消息,并且游戏无法显示。无效标签表明返回函数中第二行开头的“y”是错误的罪魁祸首。

player.midpoint = function()
{
return
{
x: this.x + this.width/2,
y: this.y + this.height/2
};
};

但是,当我取出这段代码时,游戏运行正常,只有当我按空格键射击时,游戏才会卡住,因为它需要上述函数来发射子弹。

最佳答案

Automatic Semicolon Insertion打你。您的代码被解析为

player.midpoint = function() {
return;
{
x: this.x + this.width/2,
y: this.y + this.height/2
}
};

其中大括号形成一个 block ,xylabels of 语句 - 以及 y: 之前的尾随逗号是语法错误。

您需要将返回的表达式放在与返回相同的行中:

player.midpoint = function() {
return {
x: this.x + this.width/2,
y: this.y + this.height/2
};
};

关于javascript - 语法错误: invalid label with javascript game,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23295402/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com