1

I'm using this code to draw a random pics from this 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"),}

with this function love.graphics.draw({FishImages.image1#--I guess the modification is here },pos.x,pos.y)

so how to pick up a random key from a table in Lua ?

1 Answer 1

1

math.random(1,4) generates a random integer in the range 1 to 4. So you can use:

FishImages['image' .. tostring(math.random(1,4))]
1
  • thanks , its working but not like the way i need, love2d calling the love.graphics.draw every moving pixel so i get the 4 images moving at the same time Commented Jan 10, 2015 at 11:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.