physics.stop() cannot be called during collision event

Hello!

I am fairly new to Corona and am developing a Lua version of the arcade game “Space Shooter.” Everything works well except when the user is out of lives and the screen needs to be moved on to the next scene. Then I get the error: “physics.stop() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event”

Here is the code for my scene:hide function:

function scene:hide( event )

    local sceneGroup = self.view

    

    local phase = event.phase

    

    if event.phase == “will” then

        – Called when the scene is on screen and is about to move off screen

        –

        – INSERT code here to pause the scene

        – e.g. stop timers, stop animation, unload sounds, etc.)

        

        – stop background music

        audio.stop(playMusic)

        

        – disable tap

        --Runtime:remvoeEventListener(“tap”, shoot)

        background:removeEventListener(“touch”, touchFunc)

        

        – disable physics

        player:removeEventListener(“collision”, collisionEvent)

        physics.stop()

        

    elseif phase == “did” then

        – Called when the scene is now off screen

    end

    

end

Any help would be greatly appreciated!

Thank you,

Liv :slight_smile:

Hi Liv,

I can help you out here… physics is like my pet passion for some reason.  :wink:

What’s essentially happening is that, when you remove the collision listener, the physics engine is probably still resolving some internal collision-related math when you call “physics.stop()” on the next line. At its core, the physics engine doesn’t like being interrupted during that process.

There are two solutions to this:

  1. If you plan to keep using the physics engine (i.e. you’re going to return to the game at some point), use “physics.pause()” instead of “physics.stop()”. Stopping the physics engine is a somewhat severe call, as it effectively destroys the entire physics world. Sometimes that can be useful, but if you’re going to return to using it again somewhat soon, then it’s usually best to just pause it, then resume it later when you’re ready to put it back in action.

  2. If complete stopping/destroying the physics world is what you desire, call “physics.stop()” following a very short timer (like 50 milliseconds). The following code is pretty typical for that:

[lua]

timer.performWithDelay( 50, function() physics.stop(); end )

[/lua]

Hope this helps,

Brent

Wow thank you so much!

All solved now. You were really helpful.

Hi Liv,

I can help you out here… physics is like my pet passion for some reason.  :wink:

What’s essentially happening is that, when you remove the collision listener, the physics engine is probably still resolving some internal collision-related math when you call “physics.stop()” on the next line. At its core, the physics engine doesn’t like being interrupted during that process.

There are two solutions to this:

  1. If you plan to keep using the physics engine (i.e. you’re going to return to the game at some point), use “physics.pause()” instead of “physics.stop()”. Stopping the physics engine is a somewhat severe call, as it effectively destroys the entire physics world. Sometimes that can be useful, but if you’re going to return to using it again somewhat soon, then it’s usually best to just pause it, then resume it later when you’re ready to put it back in action.

  2. If complete stopping/destroying the physics world is what you desire, call “physics.stop()” following a very short timer (like 50 milliseconds). The following code is pretty typical for that:

[lua]

timer.performWithDelay( 50, function() physics.stop(); end )

[/lua]

Hope this helps,

Brent

Wow thank you so much!

All solved now. You were really helpful.