如何用Lua,Love2D中的表替换打印的字符串

时间:2015-01-10 22:41:45

标签: lua love2d

我一直试图在"模式"中打印第二个字符串。我按下鼠标键时的表格。 但当我点击按钮时,它会打印"模式:1"而不是"模式:circle"。这是我的代码:

function love.load()
mode = {"square", "circle"}
currentMode = mode[1]
end

function nextMode()
currentMode = next(mode)
print(currentMode)
end

function love.draw()
love.graphics.print("mode: " .. currentMode, 10, 10)
end

function love.mousepressed(x, y, button)
if button == "r" then
nextMode()
end
end

有人可以告诉我我做错了什么并纠正了我吗?

1 个答案:

答案 0 :(得分:2)

next返回索引和值,但不保留任何状态,因此有第二个参数可以传递上一个索引。

一般nextIndex, nextValue = next(mytable, previousIndex)

在您的示例中,我们为currentMode分配了nextIndex,这是“圈子”的索引,是值2.

这是一个有效的例子:

function love.load()
   mode = {"square", "circle"}
   currentIndex, currentMode = next(mode)
end


function love.mousepressed(x, y, button)
    if button == "r" then
       currentIndex, currentMode = next(mode, currentIndex)
    end
end

function love.draw()
   love.graphics.print("mode: "..currentMode, 10, 10)
end
相关问题