Love2d:从表中随机选择元素

我正在尝试从一个表中随机获取一个项目。我在网上搜索过,但是我找到的所有代码都没有起作用。我的表格看起来像这样:

section = {a,b}
love.graphics.newImage("/Images/a.png")
love.graphics.newImage("/Images/b.png")
love.graphics.draw(section[imath.random(#section)], x, y)

我需要从这个表格中随机选择一个项目。

点赞
用户107090
用户107090

尝试这个:

item = section[math.random(#section)]

在你的例子中:

section = {
   love.graphics.newImage("/Images/a.png"),
   love.graphics.newImage("/Images/b.png"),
}
love.graphics.draw(section[math.random(#section)], x, y)
2015-03-02 17:41:18