Physics error

I am trying to create an explosion sprite sheet on bomb impact and add physics to it so i can know which players are on it’s range/colliding with it. But i get error when i try to add physics to explosion sprite. it says…

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

My code:

local function bulletCollision( event ) if ( event.phase == "began" ) then if(event.object1.type == "bullet" and event.object2 ~= player)then elseif(event.object2.type == "bombExplode" and (event.object1.type == "bombSurface" or event.object2.type == "player"))then print("bomb Explosion") local xPos, yPos = event.object2.x, event.object2.y event.object1:removeSelf() event.object1 = nil event.object2:removeSelf() event.object2 = nil --explosion local sequenceData = { { name="explode", frames={ 1,2,3,4,5,6,7,8,9,10 }, time=1000, loopCount = 1 } } local explosionInfo = require("explosion") local explosionSheet = graphics.newImageSheet( "explosion.png", explosionInfo:getSheet() ) local explosion = display.newSprite( explosionSheet , sequenceData ) physics.addBody(explosion, { density = 1.0, friction = 0.3, bounce = 0, isSensor = true, radius = 70 } ) explosion.x = xPos explosion.y = yPos - 30 --it should be rethinked explosion.xScale = 1.6 explosion.yScale = 1.6 explosion:play ( ) explosion:addEventListener ( "sprite", explosionRemove ) end end end

Then i added a timer to perform the explosion a few mili seconds later but it also failed and gave the same error…

Timer:

local function bombAddPhysicsDelay(xPos, yPos) --explosion local sequenceData = { { name="explode", frames={ 1,2,3,4,5,6,7,8,9,10 }, time=1000, loopCount = 1 } } local explosionInfo = require("explosion") local explosionSheet = graphics.newImageSheet( "explosion.png", explosionInfo:getSheet() ) local explosion = display.newSprite( explosionSheet , sequenceData ) physics.addBody(explosion, { density = 1.0, friction = 0.3, bounce = 0, isSensor = true, radius = 70 } ) --timer.performWithDelay( 1000, bombAddPhysicsDelay(explosion) ) explosion.x = xPos explosion.y = yPos - 30 --it should be rethinked explosion.xScale = 1.6 explosion.yScale = 1.6 explosion:play ( ) explosion:addEventListener ( "sprite", explosionRemove ) end local function bulletCollision( event ) if ( event.phase == "began" ) then if(event.object1.type == "bullet" and event.object2 ~= player)then elseif(event.object2.type == "bombExplode" and (event.object1.type == "bombSurface" or event.object2.type == "player"))then print("bomb Explosion") local xPos, yPos = event.object2.x, event.object2.y event.object1:removeSelf() event.object1 = nil event.object2:removeSelf() event.object2 = nil timer.performWithDelay( 1000, bombAddPhysicsDelay(xPos, yPos) ) end end end

The whole purpose behind adding physics to explosion sprite is to know which players are in the range/is colliding with explosion.

You could just create the explosion off-screen and place it at the correct location on collision.

Hi @coolromin,

You can do as Panc software suggests, or you can also create the body after a short (10 millisecond) timer. The physics engine just needs to perform this action in the next application cycle following the collision.

Brent

You could just create the explosion off-screen and place it at the correct location on collision.

Hi @coolromin,

You can do as Panc software suggests, or you can also create the body after a short (10 millisecond) timer. The physics engine just needs to perform this action in the next application cycle following the collision.

Brent