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

时间:2015-01-10 10:32:28

标签: random lua love2d

我正在使用此代码从此表中随机绘制图片

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中的表中获取随机密钥?

1 个答案:

答案 0 :(得分:1)

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

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