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

StackOverflow https://stackoverflow.com/questions/14980371

  •  10-03-2022
  •  | 
  •  

سؤال

I'm coming from Java and trying to program iPad applications with lua and love2d. I'm fairly new and I always get this error:

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

This is my code:

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

What is the problem and what do I do to fix it and avoid it in the future? Thanks.

هل كانت مفيدة؟

المحلول

You have if...then...end...elseif.... The "end" doesn't belong there.

نصائح أخرى

John's answer is the right one, however since you're beginning I can't help giving you a bit of advice: it is much better to write this kind of code in a data-driven way. That means for instance re-writing your draw() function like this:

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

Have fun with Lua!

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top