physics.removeBody() cannot be called ... during collision event.

Hey guys!

So idea is very simple:

I have two scenes: start and game.

In start scene you can see button to go to game scene.

In game scene you can see box and ground. When box collides with ground we will go to start scene.

And so on. Where is no mechanics, box just falling and thats all.

In game scene we use 3 events: createScene, enterScene and exitScene.

createScene method creates display objects and adds them to scene view.

enterScene method adds physics bodies (using those display objects) and collision event listener.

exitScene method removes physics bodies and collision event listener.

Problem here that when collision occurs I try to change scene to start and of course exitScene method triggers and I get an error:

ERROR: physics.removeBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event.

Well timer.performWithDelay helped with removing bodies and event listeners properly, but I think its not the best workaround. Mby its not great idea to remove bodies and listeners in exitScene event.

I’ll be very happy to get some links to articles with best practises working with physics and storyboard.

You can check source code here: https://gist.github.com/2tunnels/9668318

Thank you :slight_smile:

One way is to keep all your objects in a table. Tag the objects that are to be removed during the collision, and then within an enterFrame listener loop through all the objects and remove those that have been tagged. I see your game has a small number of objects at the moment but this way as your game expands, you don’t need to worry about removing objects specifically.

Nothing wrong with removing bodies and listeners in the exitScene function, that’s what it’s there for. You just need to delay the calling of exitScene until the collision has been resolved, possibly by having a gameActive flag that is set to false when the collision triggers game over, and then is picked up the next time your enterFrame listener runs.

However you shouldn’t need to explicitly remove bodies and objects if they have been added to the scene view, Storyboard will clean all this up for you.

Thanks! Useful tips :slight_smile:

One way is to keep all your objects in a table. Tag the objects that are to be removed during the collision, and then within an enterFrame listener loop through all the objects and remove those that have been tagged. I see your game has a small number of objects at the moment but this way as your game expands, you don’t need to worry about removing objects specifically.

Nothing wrong with removing bodies and listeners in the exitScene function, that’s what it’s there for. You just need to delay the calling of exitScene until the collision has been resolved, possibly by having a gameActive flag that is set to false when the collision triggers game over, and then is picked up the next time your enterFrame listener runs.

However you shouldn’t need to explicitly remove bodies and objects if they have been added to the scene view, Storyboard will clean all this up for you.

Thanks! Useful tips :slight_smile: