In one of the Corona SDK tutorials (Can’t find the link) I found this code that generated enemies. My problem comes when I want to remove every enemy from the stage and reset the counter of enemies spawned. I can’t access the “enemy” from outside the function and I can’t find a way to remove every enemy from within the function. Anyone has a better spawning method or a way to fix my problem?
[code]
function doEnemies()
if isDead == false then
local function spawnE(params)
local enemy = display.newImage(params.image)
setPos(enemy, W + math.random(50, 75), math.random(10, H - 9))
enemy.objTable = params.objTable
enemy.index = #enemy.objTable + 1
enemy.myName = “enemy” … enemy.index
print(enemy.index)
if params.hasBody then
enemy.density = params.density or 0
enemy.friction = params.friction or 0
enemy.rotation = params.rotation or 0
enemy.bounce = params.bounce or 0
enemy.isSensor = params.isSensor or false
enemy.bodyType = params.bodyType or “dynamic”
physics.addBody(enemy, enemy.bodyType, {density = enemy.density, friction = enemy.friction, bounce = enemy.bounce, isSensor = enemy.isSensor})
end
enemy.group = params.group or nil
enemy.group:insert(enemy)
enemy.objTable[enemy.index] = enemy
function onEnemyCollision(e)
function playerHit()
if playerLife > 0 then
life[playerLife].isVisible = false
playerLife = playerLife - 1
end
if playerLife == 0 then
isDead = true
doGameOver(isDead)
player:removeSelf()
–player = nil
end
end
if e.phase == “began” then
if e.other.myName == “player” then
playerHit()
enemy:removeSelf()
enemy = nil
elseif e.other.myName == “stageEnd” then
enemy:removeSelf()
enemy = nil
end
end
end
enemy.objTable[enemy.index]:addEventListener(“collision”, onEnemyCollision)
return enemy
end
local localGroup = display.newGroup()
local spawnTable = {}
end
end
[/code] [import]uid: 154404 topic_id: 36539 reply_id: 336539[/import]