I have an array which holds 55 objects.
On launch I get the number of objects in the array, I have it displayed in a HUD. no problems so far.
However when an object is shot, the array number still holds at 55.
I have removed the objects and nill them out for sure.
I have posted the main bits below, it resides in my main.lua.
I cant get my head around it , any suggestions?
[lua] – create enemy –
function createEnemy(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[/lua]
[lua]
– enemy counter –
onEnterFrame = function( event)
local other = event.other
if (allEnemys) then
local enemyCount = 0
for k,v in pairs (allEnemys) do
enemyCount = enemyCount +1
end
if enemyCount > 20
then
– do somehting –
end
enemyCountHUD.text = enemyCount
end
return true
end
end [/lua]
[lua]Runtime:addEventListener(“enterFrame”, onEnterFrame)[/lua]
It looks like you are counting the table keys - my guess would be the keys you are using still exist, and you have set the value to nil.
Does it count correctly if you check that v is not nil?
yes, the values of the keys still exist, I checked.
I did a [lua]print(k,v)[/lua]
in the loop, when they are shot no change in count and table entries.
I can make the enemy class and main lua available if needed.
TBH, I decided to mess with classes and now I wish I hadnt, it is becoming really awkward. I originally shelved this project but figured I had done too much to leave it.
I am nearly finished but have 2 issues remaining that I just cant solve( 2 days).
Any more help would be most welcome.
**SOLVED**
ok, so basically and rather stupidly I was removing the the “object”, nilling the “object” off the screen and not removing it from the array.
I was under the impression by removing the screen object it would remove automatically from the array, no this wasn’t the case.
I just made a function call from my enemy class, to a table.remove function in the main.lua and tada all fine.
Exciting I now have a new value to play with.
thanks for all the help
Bud
It looks like you are counting the table keys - my guess would be the keys you are using still exist, and you have set the value to nil.
Does it count correctly if you check that v is not nil?
yes, the values of the keys still exist, I checked.
I did a [lua]print(k,v)[/lua]
in the loop, when they are shot no change in count and table entries.
I can make the enemy class and main lua available if needed.
TBH, I decided to mess with classes and now I wish I hadnt, it is becoming really awkward. I originally shelved this project but figured I had done too much to leave it.
I am nearly finished but have 2 issues remaining that I just cant solve( 2 days).
Any more help would be most welcome.
**SOLVED**
ok, so basically and rather stupidly I was removing the the “object”, nilling the “object” off the screen and not removing it from the array.
I was under the impression by removing the screen object it would remove automatically from the array, no this wasn’t the case.
I just made a function call from my enemy class, to a table.remove function in the main.lua and tada all fine.
Exciting I now have a new value to play with.
thanks for all the help
Bud