Colouring spawned objects

Hi,

I’m a complete newby here. I’m trying to self teach myself how to use Lua and corona so forgive me if I’m completely off here.

I’m trying to create a loop which displays 10 circles across the screen, which I have managed to do here:

local x = 10 local field = display.newGroup() local houseButtons = {} for i = 1, 10 do houseButtons = display.newCircle(75 + x, 100, 15, 15) houseButtons.id = i field:insert(houseButtons) local text = display.newText(i, 75 + x, 100, native.systemFontBold, 12 ) text:setFillColor( 1, 0, 0 ) x = x + 35 field:insert(text) houseButtons:addEventListener( "tap", touchMe ) end

I wanted to be able to colour the indivual circles, which I thought I could do with:

houseButtons[5].fill = { 0.32, 0.5, 0.2 }

but no, that doesn’t work.  I was able to colour one by using:

        if houseButtons.id == 3 then
                houseButtons.fill = { 0.32, 0.5, 0.2 }
        end

but that doesn’t seem like the best practice. What I would really like to be able to do is look up a variable number and colour the circles up to that number.

Would anyone be point me in the correct direction please

thanks

:slight_smile:

Hi @marshallp24,

Welcome to Corona! You’re not too far off here… basically you should just be creating new, individual objects for each “houseButton”.

Currently, you’re creating a Lua table (array) called “houseButtons” to (I assume) store a reference to the individual buttons you’ll create in the loop. That is a good practice, so you’re already most of the way there. The only problem is that, in the loop, you actually overwrite that Lua table you created with a new display object by the same name.

Basically, in every place inside the loop where you write “houseButtons”, replace it with:

[lua]

houseButtons[i]

[/lua]

That makes it use the loop index which you’ve set as “i”, so effectively you’ll create a new object on each iteration of the loop, and it will become part of the Lua table “houseButtons”.

Remember, “houseButtons” is the Lua table (array) that will contain all of the other individual button objects. So, you don’t want to overwrite that containing table with an individual button object.

Hope this helps,

Brent

Thank you for explaining that so well, it makes so much more sense now. I really appreciate that!

Hi @marshallp24,

Welcome to Corona! You’re not too far off here… basically you should just be creating new, individual objects for each “houseButton”.

Currently, you’re creating a Lua table (array) called “houseButtons” to (I assume) store a reference to the individual buttons you’ll create in the loop. That is a good practice, so you’re already most of the way there. The only problem is that, in the loop, you actually overwrite that Lua table you created with a new display object by the same name.

Basically, in every place inside the loop where you write “houseButtons”, replace it with:

[lua]

houseButtons[i]

[/lua]

That makes it use the loop index which you’ve set as “i”, so effectively you’ll create a new object on each iteration of the loop, and it will become part of the Lua table “houseButtons”.

Remember, “houseButtons” is the Lua table (array) that will contain all of the other individual button objects. So, you don’t want to overwrite that containing table with an individual button object.

Hope this helps,

Brent

Thank you for explaining that so well, it makes so much more sense now. I really appreciate that!