Hello,
I’m developing my first game using cocos2d-x and Box2D, and im in end of game development. However at this time i have a critical bug: If i fire too much bullets and this bullets shoot 3/ 4 enemies consecutively i’m getting an EXC_BAD_ACESS on world_->Step.
Follow the error i found that the error are end in this code inside void* b2BlockAllocator::Allocate(int32 size):
m_freeLists[index] = block->next;
That is my code for update my game scene:
void GameScene::update(float dt) {
world_->Step(dt, 8, 3);
std::vector<b2Body*> toDestroy;
// Updates all objects positions.
for (size_t i = 0; i < objects_->count(); i++)
{
Object *object = (Object*) objects_->objectAtIndex(i);
object->update(dt);
if (!VisibleRect::getVisibleRect().containsPoint(object->getPosition()))
{
toDestroy.push_back(object->getBody());
}
}
// Loop through all of the box2d bodies that are currently colliding, that we have
// gathered with our custom contact listener...
std::vector<ContactData>::iterator pos;
for (pos = contact_listener_->_contacts.begin(); pos != contact_listener_->_contacts.end(); ++pos)
{
ContactData contact = *pos;
// Get the box2d bodies for each object
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL)
{
Object *objectA = (Object *) bodyA->GetUserData();
Object *objectB = (Object *) bodyB->GetUserData();
// Bullet vs Enemy
if (objectA->getTag() == Object::kPlayerBullet && objectB->getTag() == Object::kEnemy)
{
toDestroy.push_back(bodyA);
toDestroy.push_back(bodyB);
}
// Enemy vs Bullet
else if (objectA->getTag() == Object::kEnemy && objectB->getTag() == Object::kPlayerBullet)
{
toDestroy.push_back(bodyB);
toDestroy.push_back(bodyA);
}
}
}
// Loop through all of the box2d bodies we wnat to destroy...
std::vector<b2Body*>::iterator destroyIt;
for (destroyIt = toDestroy.begin(); destroyIt != toDestroy.end(); ++destroyIt)
{
b2Body *body = *destroyIt;
Object *object = (Object *) body->GetUserData();
world_->DestroyBody(body);
this->removeChild(object, true);
objects_->removeObject(object, true);
}
}
I’m using this collision detector: http://www.cocos2d-x.org/forums/6/topics/3160
What i’m doing wrong? Anyone can help please?