You just need to pass the string variable you created into display.newImageRect. That’s the function that actually loads the image and displays it on screen. Check out the samples or any of the basic tutorials for more info on this.
[code]
local function randomImage ()
– Randomise based on the os.time())
math.randomseed( os.time() )
– Randomize the image selection
– name the images -> image1.png, image2.png etc
local imageToDisplay = “images/image” … math.random(5) … “.png”
– Show the name in the console for the example.
print (imageToDisplay)
– now use the imageToDisplay variable as the name of your image to display.
local randomImage = display.newImageRect(imageToDisplay, 20, 20)
end[/code]
Note that lua is 1 rather than 0 indexed, so you actually want math.random(5) to get five possible numbers (1 through 5 inclusive).
You also don’t need the tostring() operation as the … operator converts numbers for you, and concatenates.
Last note: newImageRect’s 2nd and 3rd parameters are in fact the width and height you want to display your image at. make sure these match up. To position the image after creating it do the following:
[code]
local randomImage = display.newImageRect(imageToDisplay, 20,20)
randomImage.x, randomImage. y = 160, 240
–OR
randomImage:translate(160, 240)
[/code] [import]uid: 87138 topic_id: 22830 reply_id: 91223[/import]