Removing objects from tables :S

Hi,

I have been wanting to create a simple mechnaic so when the enemys come in they touch the character and then they get removed from the table. I think it’s not changing the table so when it loops back through it’s nil and it’s coming up with the error about the objects being nil in the calculate angle.

--------  
  
display.setStatusBar( display.HiddenStatusBar )  
system.activate( "multitouch" )  
  
-- Load Joystick Class  
local physics = require( "physics" )  
local movieclip = require("movieclip")  
  
physics.start()  
physics.setGravity(0, 0)  
  
physics.setDrawMode( "hybrid" )  
--local background = display.newImage( "background.png" )  
  
local enemys = {}  
local enemyssize = 0  
local velocity = 0.5  
  
local characters = {}  
local characterssize = 0  
   
local function gameTick()  
 for i=1, enemyssize, 1 do  
 local angle = calculateAngle(enemys[i].x, enemys[i].y,   
 characters[1].x, characters[1].y)  
  
 enemys[i].x = enemys[i].x + (math.cos(angle) \* velocity)  
 enemys[i].y = enemys[i].y + (math.sin(angle) \* velocity)  
 end  
  
 for i=1, enemyssize, 1 do  
 if (enemys[i].deathflag == true) then  
 enemys[i]:removeSelf()  
 enemys[i] = nil  
  
 if (enemyssize \> 0) then  
 for k=i, #enemys-1, 1 do  
 enemys[k] = enemys[k+1]  
 end  
  
 enemyssize = enemyssize - 1  
 else  
 enemyssize = 0  
 end  
  
 break  
 end  
 end  
  
end  
  
function calculateAngle(x1, y1, x2, y2)  
 local angle = math.atan2(y1 - y2, x1 - x2) \* 180 / 3.14159265   
  
 return angle  
end  
  
function createEnemys( spawncount )  
 for i=1, spawncount do  
 table.insert(enemys, i, display.newImage( "enemy.png" ))   
 enemys[i].x = math.random( 20, display.contentWidth-50 )  
 enemys[i].y = math.random( 20, display.contentHeight-50 )  
 enemys[i].myName = "enemy"  
 enemys[i].deathflag = false  
  
 -- Add physics  
 physics.addBody( enemys[i] )  
 enemys[i].isFixedRotation = true  
 i = i + 1  
 end  
  
 enemyssize = enemyssize + spawncount  
end  
  
function createCharacters( spawncount )  
 for i=1, spawncount do  
 table.insert(characters, i, display.newImage( "character.png" ))   
 characters[i].x = 50  
 characters[i].y = 50  
 characters[i].myName = "character"  
  
 -- Add physics  
 physics.addBody( characters[i] )  
 characters[i].isFixedRotation = true  
 i = i + 1  
 end  
  
 characterssize = characterssize + spawncount  
end  
  
local function onCollision( event )  
 if ( event.phase == "began" ) then  
 if (event.object1.myName == "enemy" and   
 event.object2.myName == "character") then  
 event.object1.deathflag = true  
 end  
 end  
end  
  
Runtime:addEventListener( "collision", onCollision )  
Runtime:addEventListener( "enterFrame", gameTick )  
  
createEnemys(5)  
createCharacters(1)  

I have tryed using this method for deleting aswell as using table.remove(enemys, i) and also various other methods with zero luck.

Any help would be appricated.

Thanks. [import]uid: 22223 topic_id: 15569 reply_id: 315569[/import]

Just a thought I could be wrong but wouldnt hurt to try. To iterate through a whole table you need a # sign

your Current Code
[lua] for i=1, enemyssize, 1 do[/lua]

newCode
[lua]for i=1, #enemyssize do[/lua] [import]uid: 39088 topic_id: 15569 reply_id: 57483[/import]

Yep i tryed that aswell but same issue :s. [import]uid: 22223 topic_id: 15569 reply_id: 57491[/import]

Not sure, but what aboout skipping nil elements of the table by doing a check in the loop? Something like:

If enemys ~= nil then

end

I have to admit I have an similar issue right now and would love to know the answer to your question. I did not have the chance to try my above suggestion on my own code!

Mo

EDIT: By the way, why are you using i = i + 1 on line 70 and 86 inside a for loop? Would that cause issues to the table since you basically skipping indexe? Maybe I am wrong but I found that strange in your code. [import]uid: 49236 topic_id: 15569 reply_id: 57497[/import]

in the lines 37 to 45, you are also trying to manipulate enemyssize to either enemyssize-1 or enemyssize=0, if you are using a loop, that would not be a good idea.

secondly, you need to read this article here to get an idea of where you are going wrong in removing your objects.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15569 reply_id: 57524[/import]