我試圖從表中隨機獲取一個項目。我在網上搜索,但我找到的所有代碼都不起作用。我的表看起來像這樣: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)
我需要從這個表中的一個隨機項目。
我試圖從表中隨機獲取一個項目。我在網上搜索,但我找到的所有代碼都不起作用。我的表看起來像這樣: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[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:45:25
@ math.random,如果你想要字母,使用'section = {「a」,「b」,「c」,「f」,「z」}'。 – lhf 2015-03-02 18:13:53
我需要這個來加載一個隨機的.png。這是代碼:'love.graphics.newImage(「/ Images/a.png」).... love.graphics.newImage(「/ Images/b.png」)love.graphics.draw(section [imath。隨機(#部分)],x,y)' – 2015-03-02 18:25:22
的可能重複[Lua中選擇從表中隨機項(http://stackoverflow.com/questions/2988246/lua-從表中選擇隨機項) – Adam 2015-03-02 17:42:34
這兩個答案都來自[Lua從表中選擇隨機項](http://stackoverflow.com/questions/2988246/lua-choose-random-item-from-table)不要與信件一起工作。 – 2015-03-02 17:55:01