Noob question: How do you make multiple groups into one?

I have two spawn functions that spawn two types of enemies and place them in two different groups, spawnedGroup and spawnedGroup2. I want to place all the enemies into a single group, which I want to call enemyLayer, so I can control all of them at once. How do I code this?
I tried:
local enemyLayer = {}enemyLayer:insert(spawnedGroup,spawnedGroup2)[/code]..but I'm getting errors. I realized this is a noob question, so any help would be much appreciated.Thanks,Steven [import]uid: 79394 topic_id: 16263 reply_id: 316263[/import]

just do this twice

enemyLayer:insert(spawnedGroup)  
enemyLayer:insert(spawnedGroup2)  

BTW, you also need to have

enemyLayer = display.newGroup()  
--not enemyLayer={}  

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16263 reply_id: 60546[/import]

I’ll try to help!

So I believe this is what you are starting with:

  • spawnedGroup

    • enemy1

    • enemy2

  • spawnedGroup2

    • enemyA

    • enemyB

And you want to end up with this

  • enemyLayer

    • enemy1

    • enemy2

    • enemyA

    • enemyB

I’ve been away from Corona a while, so code may be a bit sloppy, but the best way I can currently think of is to loop through your spawnLayers

[code]-- Your Spawn Group Setup
local spawnedGroup = display.newGroup()
local spawnedGroup2 = display.newGroup()

– Your Enemies
local enemy1 = display.newRect(10,10,10,10)
local enemy2 = display.newRect(20,20,10,10)
local enemyA = display.newRect(30,30,10,10)
local enemyB = display.newRect(40,40,10,10)
spawnedGroup:insert(enemy1)
spawnedGroup:insert(enemy2)
spawnedGroup2:insert(enemyA)
spawnedGroup2:insert(enemyB)

– Your Destination
local enemyLayer = display.newGroup()

– A loop that moves all objects from one group to another
local function moveToGroup(from, to)
while (from.numChildren > 0) do
to:insert(from[1])
end
end

– Run on both spawnedGroups
moveToGroup(spawnedGroup, enemyLayer)
moveToGroup(spawnedGroup2, enemyLayer)

print(enemyLayer.numChildren) – Prints 4[/code] [import]uid: 7721 topic_id: 16263 reply_id: 60549[/import]

Thanks JayantV and ETdoFresh for responding. I tried both methods, but was receiving errors no doubt as a result of where I was inserting the code chunks, rather than as a result of the code chunks themselves. Nevertheless, I eventually wound up simply inserting all of my enemies into the first group I created (spawnedGroup) even though they were spawned from two different spawn functions (which is why I didn’t think of doing this before). This worked ok, and proved to be the easiest thing to do, rather than creating two spawnedGroups, and then a third group to put them in. However, I still would have been incorrectly trying to do this:
spawnedGroup:insert(enemyType1,enemyType2)[/code]instead of the correct:spawnedGroup:insert(enemyType1)spawnedGroup:insert(enemyType2)[/code]so your suggestions allowed me to reach this conclusion.Thanks very much to both of you once again.Cheers,Stevenbtw: that ?:) is driving me crazy, who the heck are you? ;) [import]uid: 79394 topic_id: 16263 reply_id: 60580[/import]