2015-01-10 52 views
1

我使用这个代码绘制从该表如何从Lua中的表中选取一个随机密钥?

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

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

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

感谢,它的工作,但不喜欢我需要的方式,love2d调用love.graphics.draw每个移动的像素,所以我得到4个图像在同一时间移动 – user3741124

相关问题