1
votes

I need some help, I want to do 6 variables and in these six variables put random images, for example:

image = display.newImage("MyImage.png")
image2 = display.newIMage("MyImage2.png")

one = random(image,image2)

I can't use the function random in this case.

For this, I need help, thanks.

1

1 Answers

2
votes

Create an array of images, then select the image randomly:

images = {
    display.newImage("MyImage.png"),
    display.newIMage("MyImage2.png"),
    ...
    display.newIMage("MyImage5.png")
}
randomImage = images[math.random(1,6)]

@krs added that you can also use a table of file names and instantiate only one of them:

local imageFiles = {"MyImage.png","MyImage2.png", ... "MyImage5.png"}
local imageFile = imageFiles[math.random(#images)]
randomImage = display.newImage(imageFile)

If you chagne the image often the first technique is better since you only instantiate once, but it depends on your use case.