Love2d:来自 table 的随机元素

Love2d: random element from table

我正在尝试从 table 中随机获取一件物品。我在网上搜索过,但我找到的所有代码都不起作用。我的 table 看起来像这样:

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

我需要一件随机物品table。

试试这个:

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)