Removing Objects From An Array Interesting Issue

I have a grid of aliens ( think Space invaders).

I am a the stage that I now need to remove them.

They are created in a for loop and added to a table.

Now then this is the interesting part.

when I remove them on gameover, the aliens that were shot remain the others are removed, this is not always the case as sometimes fewer will remain.

So I am not removing them correctly, here  are the main bits

I have counter that counts the aliens in the array, this works fine; I use the below to remove the enemy from the array when its shot.

[lua]table.remove(allEnemys, enemy)[/lua]

here is the stripped down code, any help would be awesome, I read somewhere i may have to remove them in reverse order? cant find the post though.I could supply the class and main in full if needed.

CREATE ENEMY

[lua]

–Enemy.lua–

function enemys:init(xloc, yloc, row )

    self.image = display.newSprite(budSprites.sheets.invaders[row], budSprites.sequences.normRun)
    self.image:setReferencePoint( CenterReferencePoint )
    self.image.x = xloc
    self.image.y = yloc
    self.image.name = “enemy”
    local enemyCollisionFilter = { categoryBits = 64, maskBits = 111}
    physics.addBody( self.image,  {isSensor = true, filter = enemyCollisionFilter})

end [/lua]

PLACE INVADER

[lua]

– main.lua –

function createInvader(x, y, row)

  scene = display.newGroup()
  allEnemys = {}
  local enemys = require(“modules.enemy”)
  for j = 1, 5 do
    for i = 1, 11 do
      allEnemys[#allEnemys + 1] = enemys:new()
      allEnemys[#allEnemys]:init(i * 60, j * 70 + 70,j)
      allEnemys[#allEnemys]:start()
    end
  end
end

[/lua]

REMOVAL CODE

[lua]

– main.lua–

function gameOver()
   Runtime:removeEventListener(“enterFrame”, onEnterFrame) 
   for k,v in pairs( allEnemys ) do  
        v:cleanup()                  
    end
end[/lua]

CLEANUP FUNCTION

[lua]

–  ENEMY.LUA –

function enemys:cleanup()
        Runtime:removeEventListener( “enterFrame”, self )
        if pcall(function() self.image:removeSelf() end) then
    else
      self.image = nil
    end
end

[/lua]

Try this reverse order removal code:

[lua]

for i = #allEnemys,1,-1 do

     allEnemys[i]:cleanup()

end

[/lua]

HBI, I tried your suggestion but I get the same issue, when I don’t shoot anything the screen clears ok but I notice the array counter still displays the full amount of aliens.

when I shoot an alien the array counter will decrease,  but when I clear the screen the shot aliens remain.

Any more suggestions would help I am very stuck on this one.

Am I removing the alien from the array correctly? as above? the counter says so and if print(k,v) I see they are not there.

Very frustrating.

You didn’t use table.remove right. 

second argument is position in table(see docs), so you remove nothing

so you need something like this

[lua]function removeEnemy(enemy)
  for i, v in ipairs(allEnemys) do
    if v == enemy then
      enemy:cleanup()
      table.remove(allEnemys, i)
      break

    end
  end
end[/lua]

Try this reverse order removal code:

[lua]

for i = #allEnemys,1,-1 do

     allEnemys[i]:cleanup()

end

[/lua]

HBI, I tried your suggestion but I get the same issue, when I don’t shoot anything the screen clears ok but I notice the array counter still displays the full amount of aliens.

when I shoot an alien the array counter will decrease,  but when I clear the screen the shot aliens remain.

Any more suggestions would help I am very stuck on this one.

Am I removing the alien from the array correctly? as above? the counter says so and if print(k,v) I see they are not there.

Very frustrating.

You didn’t use table.remove right. 

second argument is position in table(see docs), so you remove nothing

so you need something like this

[lua]function removeEnemy(enemy)
  for i, v in ipairs(allEnemys) do
    if v == enemy then
      enemy:cleanup()
      table.remove(allEnemys, i)
      break

    end
  end
end[/lua]