如何从 Lua 中的 table 中获取随机密钥?

How to pick up a random key from a table in Lua?

我正在使用此代码从中随机抽取图片 table

FishImages = {image1 = love.graphics.newImage("bg/fish1.png"),
            image2 = love.graphics.newImage("bg/fish2.png"),
            image3 = love.graphics.newImage("bg/fish3.png"),
            image4 = love.graphics.newImage("bg/fish4.png"),}

有这个功能love.graphics.draw({FishImages.image1#--I guess the modification is here },pos.x,pos.y)

那么如何从 Lua 中的 table 中获取随机密钥?

math.random(1,4) 生成一个范围在 14 之间的随机整数。所以你可以使用:

FishImages['image' .. tostring(math.random(1,4))]