Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Love2d:表中的随机元素_Arrays_Lua_Lua Table_Love2d - Fatal编程技术网

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)