physics addbody error

Hi! I am making sort of a brick breaker game without the bricks… in which you need to touch the yellow part of the boundaries in order the get score and avoid the red part( obstacles ) to stay alive. I am having touble when calling addbody right after a collision.

ERROR: C:\Users\איתי חן\Desktop\Bricks\game2.lua:42: physics.addBody() cannot be called when the world is locked and in the middle of number crunching, such as during a collision event

this is my spawnObs func: 

function spawnObs( timesToPerform )     for i = 1, timesToPerform do         local obs;          local obsW = rand( 20, 60 );         local maxX = \_W - obsW;         local obsX = rand( obsW, maxX );         obs = display.newRect( obsX, 0, obsW, 5 )         obs:setFillColor( 255, 0, 0 );         obs.id = "obs";         physics.addBody( obs, "static", {             isSensor = true;         });         spawnedObjects[#spawnedObjects + 1] = obs;     end end

function removeSpawnedObjects()     for i = #spawnedObjects, 1, -1 do         display.remove( table.remove( spawnedObjects, i ) );     end end

when the ball touches “roof”( top of the screen ) I remove the spawnedObs and calling the spawnObs again…

function onBallCollision( self, event ) &nbsp; &nbsp; if( event.phase == "began" ) then &nbsp; &nbsp; &nbsp; &nbsp; print( event.other.id ); &nbsp; &nbsp; &nbsp; &nbsp; if( event.other.id == "obs" ) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gameOver(); &nbsp; &nbsp; &nbsp; &nbsp; elseif( event.other.id == "paddle" or event.other.id == "roof") then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( event.other.id == "paddle" ) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score = score + 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scoreText.text = score; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if( obsNum \<= 3 and score % 10 == 0 ) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obsNum = obsNum + 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; removeSpawnedObjects(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spawnObs( obsNum ); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ySpeed = -( ySpeed ); &nbsp; &nbsp; &nbsp; &nbsp; elseif( event.other.id == "wall" ) then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xSpeed = -( xSpeed ); &nbsp; &nbsp; &nbsp; &nbsp; end &nbsp; &nbsp; end end

Any idea how can i fix the problem…?

Thanks in advance,

Itay

Physics docs https://docs.coronalabs.com/guide/physics/collisionDetection/index.html quote:

Currently, the Box2D physics engine is liable to crash during a collision if Corona code attempts to modify objects still involved in the collision. This is because Box2D is still working out the iterated mathematics on these objects. However, your collision handler may set a flag or include a time delay via timer.performWithDelay() so that the action can occur in the next application cycle or later.

So you need to put a small delay on the removeSpawnedObjects call in your collision handler.

Thank alot!

Physics docs https://docs.coronalabs.com/guide/physics/collisionDetection/index.html quote:

Currently, the Box2D physics engine is liable to crash during a collision if Corona code attempts to modify objects still involved in the collision. This is because Box2D is still working out the iterated mathematics on these objects. However, your collision handler may set a flag or include a time delay via timer.performWithDelay() so that the action can occur in the next application cycle or later.

So you need to put a small delay on the removeSpawnedObjects call in your collision handler.

Thank alot!