I have a few colours stored inside a table
local xLocation = \_W/2 -- \_W/2 & \_H/2 is basically centre of screen local yLocation = \_H/2 - 100 local allColors = { {name="Red", image = "Shapes/redDiamond.png",height = 43, width = 43, x=xLocation +10, y=yLocation }, {name="Blue", image = "Shapes/blueDiamond.png", height = 43, width = 43, x=xLocation- 10, y=yLocation }, {name="Green", image = "Shapes/greenDiamond.png", height = 43, width = 43, x=xLocation+30, y=yLocation}, {name="Yellow", image = "Shapes/yellowDiamond.png", height = 43, width = 43, x=xLocation-30, y=yLocation}, {name="Purple", image = "Shapes/Purple.png", height = 43, width = 43, x=xLocation-60, y=yLocation} }
And I have another table for storing chosen colours
local choosenColors = {} -- Stores 4 random colours from allColors table
Finally the function that picks the 4 random colors, inserts them into choosenColours, and then displays them.
while #choosenColors \< 4 do n = math.random(1, #allColors) table.insert(choosenColors, allColors[n]) for i=1, #(choosenColors) do local s = display.newImageRect(choosenColors[i].image, choosenColors[i].height, choosenColors[i].width) s.x = choosenColors[i].x s.y = choosenColors[i].y baseGroup:insert(s) end end --table.remove(allColors, n) for k, v in pairs(choosenColors) do print(k, v) end
The issue
I’d like to point out that I am aware I have hard-coded all the positions manually, this was just for testing purposes to see if the function was displaying everything inside the chosenColours function.
However, now that it is working, im stuck trying to figure out how I would go about putting the objects in a line equally spaced. For example…
O O O O
Also how to then stop duplicates inside the chosenColours table from happening.
Anyone can help me out on this would be greatly appreciated.