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]