Arrange shapes in a line equally and stop duplication

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. 

1.  calculate “s.x” as f.e. #chosenColors*50+50

  1. shuffle allColors, then take the first 4 as chosenColors

Hi dip,

Try 

-- Use your own values local spacing, startX = 20, 100 s.x = startX + ( i - 1 ) \* ( s.width + spacing )

Have a nice day:)

ldurniat

Thanks guys, this is perfect really appreciate it.

Im just wondering now how I can stop colours of the same type from entering the chosenColors table as I am now getting 2 reds and 2 greens, which isn’t what I want. 

I would just shuffle your allColors table, and instead of taking random entries into chosenColors, take the first four.

Oh as @davebollinger mentioned above. I was confused but do see how it works now and will give it a go.

Thanks for the help everyone :slight_smile:

Why oh why have I never heard of Rosettacode before???   I’ve only had a quick browse but have already found some really cool code :slight_smile:

Thanks Dave :slight_smile:

Sorry to bump this thread again. Just stuck trying to figure out how to make the table objects in the loop unique. So each one uses the name etc. 

Ideally I would like the player to hit each object individually and I can make certain objects transition away etc. At the moment when I do a transition using ‘s’ as the object it only takes the last object created. How can I make it so I can select any of the 4 I want? 

I hope that makes sense. Cheers

keep a reference to “s” inside chosenColors, fe

 chosenColors[i].s = s -- and remember to clean this up later

It isn’t working and Im sure its something to do with the way set it up or the way inserted my colours into the table. I will post it up so you guys can have a look when you get time but I will keep at it until then.

 while #choosenColors \< 4 do -- \*\*\* INSERTS THE FIRST 4 COLOURS FROM ALLCOLOURS{} INTO CHOOSENCOLOURS{} \*\*\* -- table.insert(choosenColors, allColors[1]) table.insert(choosenColors, allColors[2]) table.insert(choosenColors, allColors[3]) table.insert(choosenColors, allColors[4]) for i=1, #(choosenColors) do -- \*\*\* GETS THE DETAILS FROM THE TABLE AND DISPLAYS ITS PROPERTIES \*\*\* -- idx = idx + 1 s = display.newImageRect(choosenColors[i].image, choosenColors[i].height, choosenColors[i].width) s.x = (i-1)\*65+60 s.y = choosenColors[i].y s.name = choosenColors[i].name s:addEventListener("tap", revealColor) choosenColors[i].s = s end end

And then the function with the transition to which I want to manipulate them individually 

function newDelay( event ) transition.to(choosenColors[1],{time=vanishTime, alpha=0.01, xScale = 0.1, yScale = 0.1})

You need reference to object. Use  choosenColors[1].s as first argument for transition e.g.

transition.to(choosenColors[1].s,{time=vanishTime, alpha=0.01, xScale = 0.1, yScale = 0.1})&nbsp;

Note:

  • You omitted local key word in definition of newDelay function so you make it global.
  • Consider make s variable local. 

Read more :

Good luck:)

1.  calculate “s.x” as f.e. #chosenColors*50+50

  1. shuffle allColors, then take the first 4 as chosenColors

Hi dip,

Try 

-- Use your own values local spacing, startX = 20, 100 s.x = startX + ( i - 1 ) \* ( s.width + spacing )

Have a nice day:)

ldurniat

Thanks guys, this is perfect really appreciate it.

Im just wondering now how I can stop colours of the same type from entering the chosenColors table as I am now getting 2 reds and 2 greens, which isn’t what I want. 

I would just shuffle your allColors table, and instead of taking random entries into chosenColors, take the first four.

Oh as @davebollinger mentioned above. I was confused but do see how it works now and will give it a go.

Thanks for the help everyone :slight_smile:

Why oh why have I never heard of Rosettacode before???   I’ve only had a quick browse but have already found some really cool code :slight_smile:

Thanks Dave :slight_smile:

Sorry to bump this thread again. Just stuck trying to figure out how to make the table objects in the loop unique. So each one uses the name etc. 

Ideally I would like the player to hit each object individually and I can make certain objects transition away etc. At the moment when I do a transition using ‘s’ as the object it only takes the last object created. How can I make it so I can select any of the 4 I want? 

I hope that makes sense. Cheers

keep a reference to “s” inside chosenColors, fe

 chosenColors[i].s = s -- and remember to clean this up later

It isn’t working and Im sure its something to do with the way set it up or the way inserted my colours into the table. I will post it up so you guys can have a look when you get time but I will keep at it until then.

 while #choosenColors \< 4 do -- \*\*\* INSERTS THE FIRST 4 COLOURS FROM ALLCOLOURS{} INTO CHOOSENCOLOURS{} \*\*\* -- table.insert(choosenColors, allColors[1]) table.insert(choosenColors, allColors[2]) table.insert(choosenColors, allColors[3]) table.insert(choosenColors, allColors[4]) for i=1, #(choosenColors) do -- \*\*\* GETS THE DETAILS FROM THE TABLE AND DISPLAYS ITS PROPERTIES \*\*\* -- idx = idx + 1 s = display.newImageRect(choosenColors[i].image, choosenColors[i].height, choosenColors[i].width) s.x = (i-1)\*65+60 s.y = choosenColors[i].y s.name = choosenColors[i].name s:addEventListener("tap", revealColor) choosenColors[i].s = s end end

And then the function with the transition to which I want to manipulate them individually 

function newDelay( event ) transition.to(choosenColors[1],{time=vanishTime, alpha=0.01, xScale = 0.1, yScale = 0.1})