【发布时间】:2017-01-01 11:55:03
【问题描述】:
我对 Lua 中的 elseif 块有点困惑。在official Lua tutorial thing 上,它显示整个 if 块只需要一个末端。但是,在我的代码中,每当我没有为我的 if 中的每个 elseif 添加关闭时,我都会收到错误消息。这是我的全部代码:
if direction == 1 then
snakeHead.y = snakeHead.y - 2
else if direction == 2 then
snakeHead.x = snakeHead.x + 2
else if direction == 3 then
snakeHead.y = snakeHead.y - 2
else if direction == 4 then
snakeHead.x = snakeHead.x - 2
end
end
end
end
此外,此代码在 Love2D 中,尽管这可能与问题完全无关。
【问题讨论】:
-
尝试将
else if更改为elseif,即。丢失两个关键字之间的空格。 -
您正在使用嵌套的 if 语句创建
else块,而不是使用elseif