Arrays Love2d:表中的随机元素
我正试图从表中随机获取一个项目。我在网上搜索过,但是我找到的所有代码都不起作用。我的桌子看起来像这样:Arrays Love2d:表中的随机元素,arrays,lua,lua-table,love2d,Arrays,Lua,Lua Table,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) 我需要此表中的一个随机项。尝试以下操作: item = section[
section = {a, b}
love.graphics.newImage("/Images/a.png")
love.graphics.newImage("/Images/b.png")
love.graphics.draw(section[imath.random(#section)], x, y)
我需要此表中的一个随机项。尝试以下操作:
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)
来自的两个答案的可能重复项不适用于字母。@math.random,如果您想要字母,请使用
部分={“a”、“b”、“c”、“f”、“z”}
。我需要这个来加载random.png。这是代码:love.graphics.newImage(“/Images/a.png”)。。。。love.graphics.newImage(“/Images/b.png”)love.graphics.draw(部分[imath.random(#部分)],x,y)