gpt4 book ai didi

c++ - Box2d 销毁集合中的所有对象

转载 作者:太空宇宙 更新时间:2023-11-04 11:51:30 25 4
gpt4 key购买 nike

我正在使用 cocos2d 和 box2d,我有最多 5 个 b2bodies 需要同时销毁。它们都被添加到一个集合中 std::set<b2Body*>row1RedArray;并由 row1RedArray.insert(spriteBody); 添加,我已经通过迭代删除了数组中的所有项目,但是当我在删除它们后触摸屏幕时,我的程序就崩溃了。我是否正确地销毁了 b2Bodies?

//if that array is empty, then remove all objects from this array (row4)

if ((row4BlueArray.count == 0) && (row4.count >> 0) && (row4Removed == NO)) {
std::set<b2Body *>::iterator pos04;
for(pos04 = row4RedArray.begin(); pos04 != row4RedArray.end(); ++pos04) {
b2Body *rowBody = *pos04;
if (rowBody->GetUserData() != NULL)
{
for (CCSprite* sprite4 in row4) {
[self removeChild:sprite4 cleanup:YES];
}
//Adding the b2Body to the toDelete Set and then removing it from the set of b2Bodies
toDestroy.insert(rowBody);
row4RedArray.erase(rowBody);
row4Removed = YES;
}
}
}
std::set<b2Body *>::iterator pos2;
for(pos2 = toDestroy.begin(); pos2 != toDestroy.end(); ++pos2) {
b2Body *body = *pos2;
if (body->GetUserData() != NULL)
{
//Then removing the b2Body completely (this is all at the end of the tick method)
_world->DestroyBody(body);
}
}

最佳答案

Captain Obvlious 在评论中的解决方案很明显,但并不正确。实体应该被 world->DestroyBody() 销毁。它需要简单地遍历主体,并通过调用此方法销毁每个主体(从不为 b2Body、b2Fixture 或 b2Joint 调用 delete)。没有办法一次将它们全部摧毁。

关于c++ - Box2d 销毁集合中的所有对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18024811/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com