我有一个奇怪的问题。 当我的球员与身体发生碰撞时,他的镜头会毫无问题地摧毁它,但是,当它立即击中两个身体时,它会崩溃。
射击运动员
b2Body *shooting = [_lhelper newBodyWithUniqueName:@"shoot" world:_world];
CCSprite *shootingSprite = (CCSprite *)shootingBody->GetUserData();
shootingBody.position = pos;
shootingBody->SetTransform(b2Vec2(pos.x/PTM_RATIO,
pos.y/PTM_RATIO),
CC_DEGREES_TO_RADIANS(angle));
接触
if([spriteA tag] == ENEMY && [spriteB tag] == SHOT)
{
int animIdx = [(NSNumber*)[spriteA userData] intValue];
if(animIdx < 2)
{
[spriteA setTextureRect:MY_RECTS[animIdx]];
[spriteA setUserData:[NSNumber numberWithInt:animIdx+1]];
}
else
{
[objectThatWillBeDeleted addObject:[NSValue valueWithPointer:bodyA]];
}
[objectThatWillBeDeleted addObject:[NSValue valueWithPointer:bodyB]];
}
else if([spriteB tag] == ENEMY && [spriteA tag] == SHOT)
{
int animIdx = [(NSNumber*)[spriteB userData] intValue];
if(animIdx < 2)
{
[spriteB setTextureRect:MY_RECTS[animIdx]];
[spriteA setUserData:[NSNumber numberWithInt:animIdx+1]];
}
else
{
[objectThatWillBeDeleted addObject:[NSValue valueWithPointer:bodyB]];
}
[objectThatWillBeDeleted addObject:[NSValue valueWithPointer:bodyA]];
}
更新
std::vector<Contact>::iterator pos;
for(pos = _contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos)
{
Contact contact = *pos;
//[self checkBodies2:&contact];
}
for(NSValue* val in objectThatWillBeDeleted)
{
b2Body* body = (b2Body*)[val pointerValue];
[_lhelper removeBody:body];
}
[objectThatWillBeDeleted removeAllObjects];
}
}
我不明白错误在哪里。
答案 0 :(得分:1)
我不明白错误在哪里,因为你没有说它崩溃的地方:) 但我猜你想要两次摧毁同一个身体。
当镜头在同一时间步中击中两个敌人时,镜头体会被添加到objectThatWillBeDeleted列表两次。你只需要在销毁它的内容之前使该列表唯一。