Changing the fill of the child of a group in a table

This is my first post! :slight_smile: Ive been happily plugging away with Corona for while now, and have run into a bit of a wall. Heres what I want to do: I have spawns that consist of several rects in a group. The spawns are kept in a table. After the spawn is generated I want to go back in and apply fills to the rect’s as necessary. How can I ā€œpointā€ to these nested elements? Any help would be awesome! Here is a simplified version of the code Ive been using to experiment on:

[lua]local spawnTable = {}

local function spawn()
Ā  Ā  local localGroup = display.newGroup()

Ā  Ā  local layer1 = display.newRect( display.contentWidth*0.5, display.contentHeight*0.5, 100, 100 )
Ā  Ā  localGroup:insert(layer1)

Ā  Ā  local layer2 = display.newRect( display.contentWidth*0.5, display.contentHeight*0.5, 100, 100 )
Ā  Ā  localGroup:insert(layer2)

Ā  Ā  return localGroup
end

spawnTable[1] = spawn()

local function CheckButtonEvent( event )
Ā  Ā  if ( ā€œendedā€ == event.phase ) then

Ā  Ā  local dude = {
Ā  Ā  Ā  Ā  type = ā€œimageā€,
Ā  Ā  Ā  Ā  filename = ā€œdude5.pngā€
Ā  Ā  }

Ā  Ā  local helmet = {
Ā  Ā  Ā  Ā  type = ā€œimageā€,
Ā  Ā  Ā  Ā  filename = ā€œhelmet.pngā€
Ā  Ā  }

Ā  Ā  spawnTable[1].layer1.fill = dude – <-- How to fill group children?
Ā  Ā  spawnTable[1].layer2.fill = helmet – <-- How to fill group children?

Ā  Ā  end
end

local checkbutton = widget.newButton
{
Ā  Ā  label = ā€œcheckā€,
Ā  Ā  id = ā€œdingleberryā€,
Ā  Ā  onEvent = CheckButtonEvent,
Ā  Ā  emboss = false,
Ā  Ā  shape=ā€œcircleā€,
Ā  Ā  radius = 20,
Ā  Ā  fillColor = { default={ 1, 1, 1}, over={ 1, 0.1, 0.7, 0.4 } },
Ā  Ā  strokeColor = { default={ 0, 0, 0, }, over={ 0.8, 0.8, 1, 1 } },
Ā  Ā  strokeWidth = 4
}

– Place the jump button
checkbutton.x = display.contentWidth / 2
checkbutton.y = display.contentHeight / 2 +100[/lua]

Groups have children and I don’t know the specifics to get you the data. However, what might make more sense is to do this:

local function spawn() &nbsp; &nbsp; local layer1 = display.newRect( display.contentWidth\*0.5, display.contentHeight\*0.5, 100, 100 ) &nbsp; &nbsp; local layer2 = display.newRect( display.contentWidth\*0.5, display.contentHeight\*0.5, 100, 100 ) &nbsp; &nbsp; return layer1, layer2 end spawnTable[1].layer1, spawnTable[1].layer2 = spawn()

Lua supports multiple return values. This would eliminate the groups and just give you a table with the spawns in it to work. If this is going to be a composer scene, you would still want to insert layer1 and layer2 into the scene’s ā€œviewā€ group.

Rob

Thanks for the reply. I think that might do the trick!

However Im getting an error. I get this ā€œAttempt to index field ā€˜?’ (a nil value)ā€ on this line - [lua]spawnTable[1].layer1, spawnTable[1].layer2 = spawn()[/lua]. I tested thisĀ [lua]local layer1, layer2 = spawn()[/lua] instead and it worked OK… Ā whats wrong with the first one?

You need to do a:

spawnTable[1] = {}

to initialize it as a table.

Thanks for the help. Having multiple returns from a function is awesome, but in my case I couldn’t get it going with everything else I had going on.Ā 

I ended up doing something like this, which seems to work:

In my spawning function:

[lua]local localGroup = display.newGroup()

localGroup.layer1 = display.newRect( 0, 0, 100, 100 )
localGroup:insert(localGroup.layer1)

localGroup.layer2 = display.newRect( 0, 0, 100, 100 )
localGroup:insert(localGroup.layer2)[/lua]

then after the function is called I can fill the rects like this:

[lua]local dude = {
type = ā€œimageā€,
filename = ā€œdude5.pngā€
}

local helmet = {
type = ā€œimageā€,
filename = ā€œhelmet.pngā€
}

spawnTable[1].layer1.fill = dude
spawnTable[1].layer2.fill = helmet[/lua]

Groups have children and I don’t know the specifics to get you the data. However, what might make more sense is to do this:

local function spawn() &nbsp; &nbsp; local layer1 = display.newRect( display.contentWidth\*0.5, display.contentHeight\*0.5, 100, 100 ) &nbsp; &nbsp; local layer2 = display.newRect( display.contentWidth\*0.5, display.contentHeight\*0.5, 100, 100 ) &nbsp; &nbsp; return layer1, layer2 end spawnTable[1].layer1, spawnTable[1].layer2 = spawn()

Lua supports multiple return values. This would eliminate the groups and just give you a table with the spawns in it to work. If this is going to be a composer scene, you would still want to insert layer1 and layer2 into the scene’s ā€œviewā€ group.

Rob

Thanks for the reply. I think that might do the trick!

However Im getting an error. I get this ā€œAttempt to index field ā€˜?’ (a nil value)ā€ on this line - [lua]spawnTable[1].layer1, spawnTable[1].layer2 = spawn()[/lua]. I tested thisĀ [lua]local layer1, layer2 = spawn()[/lua] instead and it worked OK… Ā whats wrong with the first one?

You need to do a:

spawnTable[1] = {}

to initialize it as a table.

Thanks for the help. Having multiple returns from a function is awesome, but in my case I couldn’t get it going with everything else I had going on.Ā 

I ended up doing something like this, which seems to work:

In my spawning function:

[lua]local localGroup = display.newGroup()

localGroup.layer1 = display.newRect( 0, 0, 100, 100 )
localGroup:insert(localGroup.layer1)

localGroup.layer2 = display.newRect( 0, 0, 100, 100 )
localGroup:insert(localGroup.layer2)[/lua]

then after the function is called I can fill the rects like this:

[lua]local dude = {
type = ā€œimageā€,
filename = ā€œdude5.pngā€
}

local helmet = {
type = ā€œimageā€,
filename = ā€œhelmet.pngā€
}

spawnTable[1].layer1.fill = dude
spawnTable[1].layer2.fill = helmet[/lua]