Adding

Quick question about adding displayobjects and physics bodys.

In the beginning  of my game I have a function that sets up the game area.

After winning or loosing a level I run this function again.

My question is, do I have to remove these display objects and physics bodys before I set them up again?

Or does it not matter?

I mean, I don’t want them to be created twice or more, but maybe that is not happening if the objects are the same?

And YES, I’m a newbie  :stuck_out_tongue:

They are declared as local outside the function if that matters to your answer.

[lua]leftWall = display.newRect (walls,0, 0, 4, _H)
rightWall = display.newRect (walls,_W-4, 0, 4,_H)
tak = display.newRect (walls,0, 0, _W, 4)
golv = display.newRect (walls,0, _H-4, _W, 4)

walls:toFront()

physics.addBody (leftWall, “static”, wallMaterial)
physics.addBody (rightWall, “static”, wallMaterial)
physics.addBody (tak, “static”, wallMaterial)
physics.addBody (golv, “static”,wallMaterial)[/lua]

Regards

bergapappa

Hi @bergapappa,

Once you add a physics body to a display object, it remains on the object until you explicitly remove it, or delete the object entirely (i.e. when you clear it from the screen, the physics body is cleared along with it… you don’t need to remove the body before removing the object).

So, if you remove these walls at some point, you’ll need to re-create them and re-add the physics bodies again. After being cleared from the display, they won’t remember the previous body you had applied to them.

Regards,

Brent

Tnx a lot
But what happens if I havent removed them and run this code again?
Will ther be two sets of walls or Will it still only be one?

If you run this entire routine again, it will create new objects with new physics bodies on them, because you’re displaying new objects to the screen and then adding a physics body to each one. So, two sets of walls in that case.

But, if you just run the last 4 lines on the same four objects, it won’t apply two physics bodies to them (it can’t add a new body when one exists).

TNX, learned a lot from this!

Regards

bergapappa

Hi @bergapappa,

Once you add a physics body to a display object, it remains on the object until you explicitly remove it, or delete the object entirely (i.e. when you clear it from the screen, the physics body is cleared along with it… you don’t need to remove the body before removing the object).

So, if you remove these walls at some point, you’ll need to re-create them and re-add the physics bodies again. After being cleared from the display, they won’t remember the previous body you had applied to them.

Regards,

Brent

Tnx a lot
But what happens if I havent removed them and run this code again?
Will ther be two sets of walls or Will it still only be one?

If you run this entire routine again, it will create new objects with new physics bodies on them, because you’re displaying new objects to the screen and then adding a physics body to each one. So, two sets of walls in that case.

But, if you just run the last 4 lines on the same four objects, it won’t apply two physics bodies to them (it can’t add a new body when one exists).

TNX, learned a lot from this!

Regards

bergapappa