Display Random Image

I was wandering if anyone can help me with the code to display random image.

For example I’ll have 5 different images and i want to display them randomly after completing level. I just need help with display random image code. Thanks [import]uid: 11559 topic_id: 22830 reply_id: 322830[/import]

Here is a quick example to show a random image.

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 = "image" .. tostring(math.random(4)) .. ".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.  
  
end  
randomImage ()  

Hope this helps. J. [import]uid: 29951 topic_id: 22830 reply_id: 91188[/import]

Thanks for your help
So for example if I have images in subfolder then u do this

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" .. tostring(math.random(4)) .. ".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.  
          
end  
  
-- and the for example if I need to show those random images I just gonna call   
  
randomImage ()  

Thanks. [import]uid: 11559 topic_id: 22830 reply_id: 91190[/import]

Yes, that should work. [import]uid: 29951 topic_id: 22830 reply_id: 91192[/import]

Thank you very much! [import]uid: 11559 topic_id: 22830 reply_id: 91193[/import]

do i have to display image here. Sorry im new to 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” … tostring(math.random(4)) … “.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 image1 = display.newImageRect(“images/image1.png”, 20,20)
local image2 = display.newImageRect(“images/image2.png”, 20,20)

end

– and the for example if I need to show those random images I just gonna call

[/code] [import]uid: 11559 topic_id: 22830 reply_id: 91196[/import]

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]

Thank you very much to all of you. I really appreciate it! [import]uid: 11559 topic_id: 22830 reply_id: 91298[/import]