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 ) if( event.phase == "began" ) then print( event.other.id ); if( event.other.id == "obs" ) then gameOver(); elseif( event.other.id == "paddle" or event.other.id == "roof") then if( event.other.id == "paddle" ) then score = score + 1; scoreText.text = score; if( obsNum \<= 3 and score % 10 == 0 ) then obsNum = obsNum + 1; end else removeSpawnedObjects(); spawnObs( obsNum ); end ySpeed = -( ySpeed ); elseif( event.other.id == "wall" ) then xSpeed = -( xSpeed ); end end end
Any idea how can i fix the problem…?
Thanks in advance,
Itay