Collision not Working! Please help Bullet - Enemy!

Actually, you should be able to remove the object on collision. However, there are several physics actions which you can NOT do at the exact time of collision… this is because Box2D is still working out the “math” involved with the collision, and needs to finish that before it can proceed to the next step. If you get that warning/error, you’re attempting one of those physics actions.

The solution is simple enough: you just need to perform the action after a tiny, almost imperceptible timer of 10-20 milliseconds, using “timer.performWithDelay()” (seek this out in the documentation). This allows Box2D to complete its processing, and then on the next game cycle, you can perform the physics action you need.

I get the same number crunching is still occurring during the collision event, i don’t think anything is wrong. This happens even if i change the timer to 1000(10 secs.)

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

That isn’t the correct usage of the timer API. See here:

http://docs.coronalabs.com/api/library/timer/performWithDelay.html

The timer must be inside your collision function, and it must refer to (call) the function above it. You can write “inline” timer functions too, but don’t worry about that now, just work on solving and understanding the physics.

[lua]

function removeBody()

  soldier:removeSelf()

  soldier = nil

  print(“DEAD”)

end

function onLocalCollision( self, event )

  if event.other.name == “bullet” then 

    display.remove(event.other.name)

    soldierHealth = soldierHealth - 5

    if soldierHealth == 0 then

      timer.performWithDelay( 10, removeBody )

    end

  end

end

[/lua]