Search code examples
box2dcoronasdkphysicsgame-physics

why would I get spurious collision events in this Corona SDK code snippet?


I have a game/physics prototype that seems to work fine 99 out of 100 times. But every so often on level "reset" (when use initiates this) there is an immediate collection with the player standing at the starting point which I don't expect. The physics is paused and player body is also "inactive" at this point in time so why would a collision occur during the reset phase.

Question: Is there a case where a collision would occur in my "reset level" code phase below? Wondering if because everything occurs in sequence whether this could be an issue? Does some parts of this section really need to have "performAfterDelay" perhaps.

-- PLAYER IN MOTION 
2013-09-10 11:24:56.035 Corona Simulator[6677:707] Player.isBodyActive = true


-- COLLISION OCCURS
2013-09-10 11:24:57.453 Corona Simulator[6677:707] onPlayerCollision:began:     
2013-09-10 11:24:57.453 Corona Simulator[6677:707] physics.removeBody(Player)


-- RESET LEVEL HIT (Get collision every so often on restart during this phase, but why?)
2013-09-10 11:25:01.644 Corona Simulator[6677:707] physics.pause()
2013-09-10 11:25:01.645 Corona Simulator[6677:707]   physics.addBody( Player, "dynamic")    -- add body back
2013-09-10 11:25:01.645 Corona Simulator[6677:707]   Player.isBodyActive = false            -- make inactive until after motion is started
2013-09-10 11:25:01.645 Corona Simulator[6677:707] physics.start()

I've worked the code in with the logging to given an indication of times...


Solution

  • Update: Think I have it (more testing needed) but it appear I do need to delay the physics.start to allow the 'Player.isBodyActive = false " to take effect. So last line now:

    timer.performWithDelay(100, 
       function(event)
            physics.start()
        end
    )