Cocos2d iphone 身体之间的奇怪接触

Cocos2d iphone 身体之间的奇怪接触,cocos2d-iphone,box2d,Cocos2d Iphone,Box2d,我有一个奇怪的问题。 我的球员的射门,当它与一个物体相撞时,毫无问题地摧毁了它,但是,当它同时击中两个物体时,它就崩溃了 射击运动员 b2Body *shooting = [_lhelper newBodyWithUniqueName:@"shoot" world:_world]; CCSprite *shootingSprite = (CCSprite *)shootingBody->GetUserData(); shootingBody.position = pos; shooting

我有一个奇怪的问题。 我的球员的射门,当它与一个物体相撞时,毫无问题地摧毁了它,但是,当它同时击中两个物体时,它就崩溃了

射击运动员

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]];

}
if([spriteA tag]==敌人&&[spriteB tag]==射击)
{
int animIdx=[(NSNumber*)[spriteA userData]intValue];
if(animIdx<2)
{
[spriteA setTextureRect:MY_RECTS[animIdx];
[spriteA setUserData:[NSNumber NUMBER WITHINT:animIdx+1]];
}
其他的
{
[objectThatWillBeDeleted addObject:[NSValue-valueWithPointer:bodyA]];
}
[objectThatWillBeDeleted addObject:[NSValue-valueWithPointer:bodyB]];
}
否则如果([spriteB tag]==敌人和[spriteB tag]==射击)
{
int animIdx=[(NSNumber*)[spriteB userData]intValue];
if(animIdx<2)
{        
[spriteB settxturerect:MY_RECTS[animIdx];
[spriteA setUserData:[NSNumber NUMBER WITHINT:animIdx+1]];
}
其他的
{
[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];
}
std::vector::iterator pos;
对于(pos=_contactListener->_contacts.begin();
pos!=\u contactListener->\u contacts.end();++pos)
{
触点触点=*位置;
//[自检机构2:&联系];
}
for(将被删除的对象中的NSValue*val)
{
b2Body*body=(b2Body*)[val pointerValue];
[[u lhelper removeBody:body];
}
[将删除或删除所有对象的对象];
}
}


我不知道错误在哪里。

我也不知道错误在哪里,因为您没有说它在哪里崩溃:) 但我猜你想两次摧毁同一具尸体


当射击在同一时间步中击中两个敌人时,射击体将被添加到将被删除的对象列表中两次。您只需在销毁列表内容之前使其唯一。

如果同一实体同时接触两个实体,则会报告两次碰撞接触。这是正常的行为。您应该使用NSMutableSet来确保在将被删除的对象中只能存在一个类型的对象。在添加NSMutableSet之前,您可能需要查询该实体是否存在。见: