How do I remove the physics body here?

This following code detects the collisions and removes the “soldier” but it does not get rid of the physics body around the soldier, do you think it is here where the issue is, or is there a problem with the spawn function which i will post when if you need to see it!

local function onLocalCollision(self, event) if event.other.name == "bullet" then display.remove(event.other) soldierHealth = soldierHealth - 5 if soldierHealth == 0 then --display.remove(soldier) --soldier = nil soldier:removeSelf() soldier = nil removeBody() print("DEAD") end end end soldier.collision = onLocalCollision soldier:addEventListener("collision", soldier)

display.remove(soldier)
soldier = nil

This is better then

soldier:removeSelf()

soldier = nil

However the biggest issue is trying to delete a physics object during a collision detection, box2d will usually crash, so what you need to do is wait a little bit before trying to remove or alter any of your physics objects. Something like:

transition.to(event.other,{delay=10, onComplete=function(self)

display.remove(self);self=nil

end})

Then do similar for if the soldier is dead. However other people like to add items to be removed into a ‘remove list’ first then remove all of them at the same time later on, plenty of code in the forums for doing that too.

Lastly, not sure what your removeBody() function is.

display.remove(soldier)
soldier = nil

This is better then

soldier:removeSelf()

soldier = nil

However the biggest issue is trying to delete a physics object during a collision detection, box2d will usually crash, so what you need to do is wait a little bit before trying to remove or alter any of your physics objects. Something like:

transition.to(event.other,{delay=10, onComplete=function(self)

display.remove(self);self=nil

end})

Then do similar for if the soldier is dead. However other people like to add items to be removed into a ‘remove list’ first then remove all of them at the same time later on, plenty of code in the forums for doing that too.

Lastly, not sure what your removeBody() function is.