Explosion in collision

Hello,

I’m building a game where an object is supposed to blow up when it collides with another object. To animate the explosion, I have created small “splinter” images that are supposed to fly in all directions to simulate the exploded object. The splinter can of course not be visible until the collision and I therefore wanted to create them in the collision listener. The problem is that when I try to add physics to the splinter images I get an error message saying

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

I then tried to create the images and add physics to them and “hide” them outside the screen. Apart from being a very clumsy solution, it still did not work since I got an error message when I tried to move all the splinters to the center of the explosion before applying force to make them fly around.

In other words: does anyone know how to animate an explosion with flying splinter (by added force and affected by gravity) at the time of a collision?

Thanks for your help!

Put it in a timer.performWithDelay() call with a short duration, say 10ms.

Rob

Here’s a snippet of my explosion code:

 local function pixelBurst(objEnem) local function DestroyPop(Obj) display.remove(Obj) Obj = nil end timer.performWithDelay(5, function() local function createParts() timer.performWithDelay(5, function() starRnd = math.random partDown = display.newRect(0,0,5,5) partDown:setFillColor(200,200,0) physics.addBody( partDown, "dynamic",{ density = 1.5, isSensor=true } ) partDown.x = objEnem.x+(starRnd(-5, 5)) partDown.y = objEnem.y partDown:applyForce(1\*(starRnd(-5,5)),-13, partDown.x\*(starRnd(1,10)), partDown.y) partDown:applyTorque(5\*starRnd(5,10)) transition.to(partDown, {time=1500, onComplete=DestroyPop}) gridGroup:insert(partDown) return partDown end, 15) end createParts() end, 1) end 

This works fine for me and has physics bodies in it.

Putting it all in a timer solved the problem and “Panc software’s” code snippet also gave the effect I wanted.

Thank you both for your help!

Put it in a timer.performWithDelay() call with a short duration, say 10ms.

Rob

Here’s a snippet of my explosion code:

 local function pixelBurst(objEnem) local function DestroyPop(Obj) display.remove(Obj) Obj = nil end timer.performWithDelay(5, function() local function createParts() timer.performWithDelay(5, function() starRnd = math.random partDown = display.newRect(0,0,5,5) partDown:setFillColor(200,200,0) physics.addBody( partDown, "dynamic",{ density = 1.5, isSensor=true } ) partDown.x = objEnem.x+(starRnd(-5, 5)) partDown.y = objEnem.y partDown:applyForce(1\*(starRnd(-5,5)),-13, partDown.x\*(starRnd(1,10)), partDown.y) partDown:applyTorque(5\*starRnd(5,10)) transition.to(partDown, {time=1500, onComplete=DestroyPop}) gridGroup:insert(partDown) return partDown end, 15) end createParts() end, 1) end 

This works fine for me and has physics bodies in it.

Putting it all in a timer solved the problem and “Panc software’s” code snippet also gave the effect I wanted.

Thank you both for your help!