I have an array called toRemove:
Code:
local toRemove = {}
I have a group called enemies which stores the enemy sprites and I have a collision detection function that looks like this:
Code:
local function onCollision( self, event )
--Bullet hit enemy
if self.name == "bullet" and event.other.name == "enemy" then
table.insert(toRemove, event.other)
end
end
It is getting inside the if statement. I have a game loop:
Code:
local function gameLoop ( event )
for i = 1, #toRemove do
toRemove[i].parent:remove(toRemove[i]) --This is line 158 (important to know later)
toRemove[i] = nil
end
end
This gameLoop function is invoked like so:
Code:
Runtime:addEventListener( "enterFrame", gameLoop )
When I run my program it says:
Code:
...main.lua:158: attempt to index field 'parent' (a nil value)
When I modify my game loop as follows:
Code:
local function gameLoop ( event )
for i = 1, #toRemove do
print(toRemove[i].parent)
--toRemove[i].parent:remove(toRemove[i]) --This is line 158 (important to know later)
toRemove[i] = nil
end
end
Nothing crashes (although no sprite is removed), and it prints:
Code:
table: <memory Address>
Why wont it let me remove the parent?