how to create/generate random() 4X4 images on screen and match them

Hello All!

I’m trying to generate a 4X4 grid with images and write code to help the code identify matching images. Basically a matching game. The problem I’m having the most difficulty with is randomly the 4X4 images.

My images are in a sub folder ex. root_folder --> images --> gamescreen <-- (then all my image files are in here…).

how do I build the function and for loop to randomly generate the images out of this folder onto the screen?

() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen end

You’ll need to list the image file names in a table, not read their names from ‘disk’.

Why?  On iOS, you would be OK with the latter approach.  On Android, the ‘resource’ folder cannot be iterated over because it has been packed up.  i.e. You would get this working on the simulator and it would fail on Android devices.

-- Create randomized list of image names local myImages = { "images/red.png", "images/green.png", "images/blue.png", -- ... etc. "images/yellow.png", } table.shuffle( myImages ) for i = 1, #myImages do -- ... create images here by referencing the names as myImages[i] end

This is the definition of table.shuffle():

-- == -- table.shuffle( t ) - Randomizes the order of a numerically indexed (non-sparse) table. Alternative to randomizeTable(). -- == table.shuffle = function( t, iter ) local iter = iter or 1 local n for i = 1, iter do n = #t while n \>= 2 do -- n is now the last pertinent index local k = math.random(n) -- 1 \<= k \<= n -- Quick swap t[n], t[k] = t[k], t[n] n = n - 1 end end return t end

You’ll need to list the image file names in a table, not read their names from ‘disk’.

Why?  On iOS, you would be OK with the latter approach.  On Android, the ‘resource’ folder cannot be iterated over because it has been packed up.  i.e. You would get this working on the simulator and it would fail on Android devices.

-- Create randomized list of image names local myImages = { "images/red.png", "images/green.png", "images/blue.png", -- ... etc. "images/yellow.png", } table.shuffle( myImages ) for i = 1, #myImages do -- ... create images here by referencing the names as myImages[i] end

This is the definition of table.shuffle():

-- == -- table.shuffle( t ) - Randomizes the order of a numerically indexed (non-sparse) table. Alternative to randomizeTable(). -- == table.shuffle = function( t, iter ) local iter = iter or 1 local n for i = 1, iter do n = #t while n \>= 2 do -- n is now the last pertinent index local k = math.random(n) -- 1 \<= k \<= n -- Quick swap t[n], t[k] = t[k], t[n] n = n - 1 end end return t end