Need help with displaying non-repeating, non-overlapping random images from array

Hello all.

 

In the game I’m working on, I have a table (array) of random images that appear in random different places on screen. My problem is that some images appear twice, and also some images are overlapping each other. Can you please point me in the right direction?

 

Here is my code:

-- Nonmatching Image Array local imgs = {  "1\_bamboo.png", "2\_bow.png",  "3\_bread.png",  "4\_broccoli.png", "5\_brush.png",  "6\_carrot.png",  "7\_cheese.png", "8\_corn.png", "9\_diamond.png", "10\_eggs.png" }; --Populate screen with a group of nonmatching items local nonmatchgroup = display.newGroup(nonMatchImageHolder) nonmatchgroup:addEventListener( "touch", buttonListener ) nonmatchgroup:rotate(math.random(1,359)) nonMatchImageHolder = {} -- initialize array local numOfImages = math.random(1,5) -- define how many images on screen for i=1,numOfImages do -- start for loop to go from 1 to 10 local temp = math.random(1,12) nonMatchImageHolder[i] = display.newImageRect(imgs[temp], 250, 250) nonMatchImageHolder[i].x = math.random(75, display.contentWidth - 50) nonMatchImageHolder[i].y = math.random(75, display.contentHeight - 180)  end group:insert(nonmatchgroup)

As always, any help is appreciated. Thanks!

For the first problem, consider shuffling the imgs table.

Check shuffle function here for example: http://snippets.luacode.org/snippets/Shuffle_array_145

Then you can just grab the images in whatever order they are since the order is random now

[lua]…

imgs = shuffle(imgs)

for i = 1, numOfImages do

    nonMatchImageHolder[i] = display.newImageRect(imgs[i], 250, 250)

    …

end

[/lua]

For overlapping images problem. Are you OK with having a grid? Then you can predefine the grid and fill in the slots randomly.

Jon,

Thanks so much. The shuffle() method you suggested worked splendidly! Just fyi I didn’t use randomseed. That shouldn’t cause a problem, should it?

As for the overlapping issue, I chose to forgo a fixed grid structure to make the app more organic looking. However, isn’t it just a matter of making the objects physics objects and then adjusting their radii so that they don’t “collide” so to speak? 

Here is my updated code, but the items still overlap.

imgs = shuffle(imgs) for i=1,numOfImages do -- start for loop to go from 1 to 10   nonMatchImageHolder[i] = display.newImageRect(imgs[i], 250, 250) -- create a new rectangle and store it in the nonMatchImageHolder array   nonMatchImageHolder[i].x = math.random(75, display.contentWidth - 50) -- display the image in a random x position on screen   nonMatchImageHolder[i].y = math.random(75, display.contentHeight - 180) -- display the image in a random y position on screen physics.addBody(nonMatchImageHolder[i], "static", {radius = 150}) Runtime:addEventListener("enterFrame", nonMatchImageHolder[i]) group:insert(nonmatchgroup) end

You might run into a problem on device if you don’t have random seed, I would add it before doing the shuffle. 

The physics way sounds like it should work but I have never actually used physics in Corona so I am of limited help there. 

For the first problem, consider shuffling the imgs table.

Check shuffle function here for example: http://snippets.luacode.org/snippets/Shuffle_array_145

Then you can just grab the images in whatever order they are since the order is random now

[lua]…

imgs = shuffle(imgs)

for i = 1, numOfImages do

    nonMatchImageHolder[i] = display.newImageRect(imgs[i], 250, 250)

    …

end

[/lua]

For overlapping images problem. Are you OK with having a grid? Then you can predefine the grid and fill in the slots randomly.

Jon,

Thanks so much. The shuffle() method you suggested worked splendidly! Just fyi I didn’t use randomseed. That shouldn’t cause a problem, should it?

As for the overlapping issue, I chose to forgo a fixed grid structure to make the app more organic looking. However, isn’t it just a matter of making the objects physics objects and then adjusting their radii so that they don’t “collide” so to speak? 

Here is my updated code, but the items still overlap.

imgs = shuffle(imgs) for i=1,numOfImages do -- start for loop to go from 1 to 10   nonMatchImageHolder[i] = display.newImageRect(imgs[i], 250, 250) -- create a new rectangle and store it in the nonMatchImageHolder array   nonMatchImageHolder[i].x = math.random(75, display.contentWidth - 50) -- display the image in a random x position on screen   nonMatchImageHolder[i].y = math.random(75, display.contentHeight - 180) -- display the image in a random y position on screen physics.addBody(nonMatchImageHolder[i], "static", {radius = 150}) Runtime:addEventListener("enterFrame", nonMatchImageHolder[i]) group:insert(nonmatchgroup) end

You might run into a problem on device if you don’t have random seed, I would add it before doing the shuffle. 

The physics way sounds like it should work but I have never actually used physics in Corona so I am of limited help there.