【问题标题】:lua - main.lua:18: 'end' expected (to close 'function' at line 12) near 'elseif'lua - main.lua:18: 'end' 应在'elseif' 附近(在第 12 行关闭'function')
【发布时间】:2013-02-20 22:03:38
【问题描述】:

我来自 Java,正在尝试使用 lua 和 love2d 对 iPad 应用程序进行编程。 我还很新,我总是得到这个错误:

Syntax error: main.lua:18: 'end' expected (to close 'function' at line 12) near 'elseif'

这是我的代码:

function setup()
i = 0
end

function draw()
if i == 0
then
background(0, 0, 0, 0)
i = i + 1
end
elseif i == 1
then
background(255, 0, 0, 0)
i = i + 1

elseif i == 2
then
background(0, 255, 0, 0)
i = i + 1

elseif i == 3
then
background(0, 0, 255, 0)
i = i + 1

elseif i == 4
then
background(255, 255, 0, 0)
i = i + 1

elseif i == 5
then
background(255, 255, 255, 0)
i = i + 1

elseif i == 6
then
background(0, 255, 255, 0)
i = i + 1

elseif i == 7
then
background(255, 0, 255, 0)
i = 0
end

有什么问题,我该怎么做才能解决它并在将来避免它?谢谢。

【问题讨论】:

  • 我没有任何 lua 经验,但您可能需要结束所有 elseif
  • 哇,如果 lua 是这样编码的,你已经说服我不要尝试了。

标签: lua love2d


【解决方案1】:

你有 if...then...end...elseif.... "end" 不属于那里。

【讨论】:

    【解决方案2】:

    John 的答案是正确的,但是既然你刚开始,我不禁给你一些建议:以数据驱动的方式编写这种代码要好得多。这意味着例如像这样重写您的 draw() 函数:

    backgrounds = {
      {  0,   0,   0, 0},
      {255,   0,   0, 0},
      {  0, 255,   0, 0},
      {  0,   0, 255, 0},
      {255, 255,   0, 0},
      {255, 255, 255, 0},
      {  0, 255, 255, 0},
      {255,   0, 255, 0}
    }
    
    function draw()
      background(unpack(backgrounds[i+1]))
      i = (i+1) % 8
    end
    

    用 Lua 玩得开心!

    【讨论】:

      猜你喜欢
      • 2014-06-03
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 2017-10-20
      • 2013-03-03
      • 2021-04-23
      • 1970-01-01
      • 2022-11-15
      相关资源
      最近更新 更多