I have a little problem with clearing the world from all the bodies.
During my game, when enemies die, they are removed after some time, similar to "removing bodies safely". It works just fine. But when i switch the level i want to clear all the bodies from the world, delete old level and create new one. Next level loads up, but when i interact with any enemy, it crashes, while debugger points to methods from contact listener( based on tutorial). I wanted to ask if my way of clearing all the bodies is fine, so the problem is elsewhere, like dangling pointers for enemies vector.
Here's the code for destructor of a level:
Code: Select all
Level::~Level()
{
delete ui;
delete damageResolver;
delete camera;
delete hero;
delete ls;
delete door;
std::vector<b2Body*> bodies;
for (std::vector<Enemy*>::iterator it = enemies.begin() ; it != enemies.end(); it++)
{
delete (*it);
}
for ( b2Body* b = world->GetBodyList(); b != NULL; b = b->GetNext())
{
//bodies.push_back(b);
world->DestroyBody(b);
}
/*for (std::vector<b2Body*>::iterator it = bodies.begin() ; it != bodies.end(); it++)
{
world->DestroyBody(*it);
}*/
enemies.clear();
bodies.clear();
}
Thanks in advance;)