Add a new body (physics) during an event?

Hi,

this is to further explain this thread:

http://forums.coronalabs.com/topic/37529-swap-an-image/

How do I add a physics body during an event?

When a ball hits a brick the image is being remove (to be able to create a new image) and I want that new image to also have a body.

So in a “collision” event I run this:

if event.other.value == 2 then

                print(“Value 2!”)

                local j = event.other.j

                local i = event.other.i

                local x = event.other.x

                local y = event.other.y

                

                event.other:removeSelf()

                event.other = nil

                

                brickImageTable[j][i] = display.newImage(imageSheet, 4, x, y)

                brickImageTable[j][i].value = 1

                brickImageTable[j][i].name = “brick”

                

                physics.addBody(brickImageTable[j][i], “static”, {density = 1, friction = 0, bounce = 0})

            else

                event.other:removeSelf()

                event.other = nil

                bricks.numChildren = bricks.numChildren - 1

            end

But I get this error:

ERROR: physics.addBody() cannot be called when the world is locked and in the mi

ddle of number crunching, such as during a collision event.

Is it possible, someway, to create a physics body within an event?

Thanks in advance,

Best regards,

Tomas

Can you add the block then add physics to it at a latter time
Or if physics is only used for collision with the blocks then don’t use physics and use your on collision detection
Just some more suggestions

Hi,

I want a body so that it detects collisions, right?

I can of course make some detection, from the ball, to see if it is within any range of the bricks but it can be quite a lot of bricks so that sounds more consuming than having a body?

Correct me if I’m wrong here jstrahan

What if you created the second block at se time as first but have if off screen on invisible and it show after first is destroyed

That’s genius! I will try that, thanks a lot!

Thanks, that worked!

Note: It didn’t work to change the X and Y value on the image so I did a transition.to:

Error message:

ERROR: Cannot translate an object before collision is resolved.

My “new” image to a stored X and Y value

transition.to(brickImageSecondTable[j][i], {x =x, y = y, time = 0})

Thanks!

Best regards,

Tomas

welcome

i dont normally do physics so maybe someone else may have a better solution

and heres an example of doing the collision without physics in a breakout game

Can you add the block then add physics to it at a latter time
Or if physics is only used for collision with the blocks then don’t use physics and use your on collision detection
Just some more suggestions

Hi,

I want a body so that it detects collisions, right?

I can of course make some detection, from the ball, to see if it is within any range of the bricks but it can be quite a lot of bricks so that sounds more consuming than having a body?

Correct me if I’m wrong here jstrahan

What if you created the second block at se time as first but have if off screen on invisible and it show after first is destroyed

That’s genius! I will try that, thanks a lot!

Thanks, that worked!

Note: It didn’t work to change the X and Y value on the image so I did a transition.to:

Error message:

ERROR: Cannot translate an object before collision is resolved.

My “new” image to a stored X and Y value

transition.to(brickImageSecondTable[j][i], {x =x, y = y, time = 0})

Thanks!

Best regards,

Tomas

welcome

i dont normally do physics so maybe someone else may have a better solution

and heres an example of doing the collision without physics in a breakout game