Lua代码错误(我是新手)

这段代码有什么问题?

if key == 'w' then
    if charastate == neutral then
        charamov = up
    end
    elseif charastate == lr then
        charastate = neutral then
            charamov = up
    end
end

错误是:

"unexpected symbol near 'then'"

同时,即使更改为 "and",也没有影响。

谢谢,我正在试图自学,但是非常劳累。

点赞
用户107090
用户107090

错误提示告诉你要省略最后一个 then,因为它没有与 if 对应。

正确缩进代码将有助于你看到它。

2016-01-17 19:26:12
用户4019159
用户4019159

尝试一下这段代码。在else if语句中使用了两个then

如果`key`等于`w`,则
   如果`charastate`等于`neutral`,则
      `charamov`等于`up`
   否则如果`charastate`等于`lr`,则
        `charastate`等于`neutral`
        `charamov`等于`up`
   结束
end
2016-01-17 19:39:38
用户5799596
用户5799596

我认为你在这里缺少一个 if 语句:

elseif charastate == lr then
    charastate = neutral then
        charamov = up
end

我建议你使用 and 运算符而非嵌套你的 if 语句,这样可以更容易地编辑和阅读代码:

if key == 'w' and charastate == neutral then
    charamov = up
elseif key == 'w' and charastate == lr then
    charamov = up
end
2016-01-17 20:51:43
用户3125367
用户3125367

为了帮助理解原始代码存在的问题,我对相应部分进行了注释,因此:

if key == 'w' then
    if charastate == neutral then
        charamov = up
    --[[ end BAD(1) ]]
    elseif charastate == lr then
        charastate = neutral --[[ then BAD(2) ]]
        charamov = up
    end
end

两个问题都是语法上的。1)if-elseif语句的形式为if <cond> then <statements> [elseif <statements>]+ end。请注意,在elseif之前没有end。2)then关键字不适用。原始代码在正确缩进后如下所示:

if key == 'w' then
    if charastate == neutral then
        charamov = up
    end
elseif charastate == lr then
    charastate = neutral then -- 这是语法错误
    charamov = up
end
end -- 这个end是多余的,但解析器甚至无法到达这里

(对于程序逻辑几乎没有任何意义。)

2016-01-18 08:58:25