group removal - multiple twin objects

Hi,

need some guidance here… I read about grouping objects and removing groups but can’t find a solution that fits into my project. (It’s my very first project and I’m not using scenes…)

Basically I have the play screen in wich is spawn a single enemy at a time. Every time it’s killed the ‘spawn enemy’ function is called in order to spawn another one. If game over occurs, the current enemy still on screen gets eliminated by using the relative commands in the ‘killenemy’ function (i don’t call the ‘killenemy’ function itself just implemented its commands I need into the ‘gameover’ function).

This works fine as long I want just one enemy spawn at a time. Now I would like to spawn multiple enemies at the same time wich I do by recalling the spawning function inside from within itself (at random times).

So basically now I have several enemy on screen simultaneously and they are ‘identical’ cause spawned by the same function. If I kill them individually in game it’s fine, problems arise when game over occurs: when it happens only one of them gets eliminated by the gameover function as the others remain on the gameover screen.

So I tried to put every enemy spawned inside the same group

enemies = display.newGroup( )
enemies: insert (enemy)

by inserting the above code in the spawn enemy function, and this piece of code

transition.cancel ()
    enemies:removeSelf ()
    enemies = nil

in the game over function to eliminate the group and its childs.

If I run the program with these pieces of code implemented it works smooth without returning errors as long as only one enemy is spawned at a time. Once I make the spawnenemy function spawn multiple enemies the same problem occurs. only one enemy of the group is removed once by game over.

Any idea on where the problem might reside???

Thanks.

Can you throw up a bit more of your code so we can take a look? That would help to evaluate where the problem lies.

function spawnEnemies()
    
    enemyimgs = {“img1.png”, “img2.png”, “img3.png”}
    enemyimg = enemypics[math.random(#enemypics)]
    enemy = display.newImage(enemyimg)

– to try put every enemy spawned in a group

    enemies = display.newGroup( )
    enemies: insert (enemy)
    
    enemy:addEventListener ( “tap”, killenemy )

   – a few lines more to spawn enemies randomly on the screen would be here

end

Then the game over screen:

function Gover ()

– the following three lines work when I keep on spawning only one enemy a time (*)

    transition.cancel ()
    enemies:removeSelf ()
    enemies = nil
  
    audio.fadeOut ( { channel=8, time=700 } )
    audio.play ( sndGover )
    audio.rewind ( Gover_tune )
    local endtune audio.play ( Gover_tune, { channel= 7, loops=0 } )
    audio.setVolume ( 0.4, { channel=7 })

            
    timer.performWithDelay (1000, head1)
    timer.performWithDelay (1000,  startGame)
       
end

– (*) but when I use the following multiple-spawning mechanism from within the ‘spawnenemy’ or ‘killenemy’ function, the ‘gameover’ function kills only one enemy - not the whle group:

if math.random(4) == 1 then spawnEnemy()
  end

Any idea? I don’t get significant errors from the debugging that could orient towards a solution…

Thanks!

 

You’re overwriting your enemy variable. The best way to handle this (and most other things in Lua) is with tables.

Just threw this together, didn’t test, YMMV, HTH:

 local function cleanItems(Obj) -- this is the magic function that gets removes, nils and frees up memory from your table. local i = 0; --print (#Obj) for i=#Obj,1,-1 do local child = table.remove(Obj, i) -- Remove from table if child ~= nil then --print ("object deleted = ".. i) display.remove( child ) child = nil end end display.remove( Obj ) Obj = nil collectgarbage() -- This is the Corona function for freeing memory. BE WARNED: It's very heavy so I wouldn't call it every single second. end local enemy = {} local enemyimgs = {"img1.png", "img2.png", "img3.png"} local enemyimg = enemypics[math.random(#enemypics)] local enemies = display.newGroup( ) for i=1, 5 do -- this is creating the enemies. I'm going to assume you are going to be adding additional listeners/variables/functions, so you should really create all of your enemy objects at once, and keep the ones you don't want to use offscreen and move them as needed. enemy[i] = display.newImage(enemyimg) enemies: insert (enemy[i]) enemy[i]:addEventListener ( "tap", killenemy ) end -- to try put every enemy spawned in a group cleanItems(enemy) -- called when you're exiting scene.

Can you throw up a bit more of your code so we can take a look? That would help to evaluate where the problem lies.

function spawnEnemies()
    
    enemyimgs = {“img1.png”, “img2.png”, “img3.png”}
    enemyimg = enemypics[math.random(#enemypics)]
    enemy = display.newImage(enemyimg)

– to try put every enemy spawned in a group

    enemies = display.newGroup( )
    enemies: insert (enemy)
    
    enemy:addEventListener ( “tap”, killenemy )

   – a few lines more to spawn enemies randomly on the screen would be here

end

Then the game over screen:

function Gover ()

– the following three lines work when I keep on spawning only one enemy a time (*)

    transition.cancel ()
    enemies:removeSelf ()
    enemies = nil
  
    audio.fadeOut ( { channel=8, time=700 } )
    audio.play ( sndGover )
    audio.rewind ( Gover_tune )
    local endtune audio.play ( Gover_tune, { channel= 7, loops=0 } )
    audio.setVolume ( 0.4, { channel=7 })

            
    timer.performWithDelay (1000, head1)
    timer.performWithDelay (1000,  startGame)
       
end

– (*) but when I use the following multiple-spawning mechanism from within the ‘spawnenemy’ or ‘killenemy’ function, the ‘gameover’ function kills only one enemy - not the whle group:

if math.random(4) == 1 then spawnEnemy()
  end

Any idea? I don’t get significant errors from the debugging that could orient towards a solution…

Thanks!

 

You’re overwriting your enemy variable. The best way to handle this (and most other things in Lua) is with tables.

Just threw this together, didn’t test, YMMV, HTH:

 local function cleanItems(Obj) -- this is the magic function that gets removes, nils and frees up memory from your table. local i = 0; --print (#Obj) for i=#Obj,1,-1 do local child = table.remove(Obj, i) -- Remove from table if child ~= nil then --print ("object deleted = ".. i) display.remove( child ) child = nil end end display.remove( Obj ) Obj = nil collectgarbage() -- This is the Corona function for freeing memory. BE WARNED: It's very heavy so I wouldn't call it every single second. end local enemy = {} local enemyimgs = {"img1.png", "img2.png", "img3.png"} local enemyimg = enemypics[math.random(#enemypics)] local enemies = display.newGroup( ) for i=1, 5 do -- this is creating the enemies. I'm going to assume you are going to be adding additional listeners/variables/functions, so you should really create all of your enemy objects at once, and keep the ones you don't want to use offscreen and move them as needed. enemy[i] = display.newImage(enemyimg) enemies: insert (enemy[i]) enemy[i]:addEventListener ( "tap", killenemy ) end -- to try put every enemy spawned in a group cleanItems(enemy) -- called when you're exiting scene.