2011-06-01 10 views
2

我正在爲我的遊戲使用與花栗鼠物理學的空間管理器。 我想要做的是將一個球打到一個支柱上,並讓球消失。我正在不同的位置重新創建球,以便用戶可以擊中另一個支柱。我有球形和球形精靈作爲成員變量。程序崩潰時,刪除空間管理器中的形狀 - cocos2d

以下是創建球,圓柱形狀等

ball = [smgr addCircleAt:cpv(1000,10) mass:0.5 radius:35]; 
ball->collision_type = kBallCollisionType; 

ballSprite = [cpCCSprite spriteWithShape:ball file:@"head.png"]; 
//[ballSprite autoFreeShape]; 
[self addChild:ballSprite]; 
ballSprite.spaceManager = smgr; 
//ballSprite.ignoreRotation = NO; 

cpShape *dome = [smgr addRectAt:cpv(400,500) mass:1 width:400 height:100 rotation:0]; 
dome->collision_type = kRectCollisionType; 
cpCCSprite *dome1 = [cpCCSprite spriteWithShape:dome file:@"001.png"]; 
[self addChild:dome1]; 
dome1.spaceManager = smgr; 

cpShape *pillarone = [smgr addRectAt:cpv(300,300) mass:1 width:45 height:194 rotation:0]; 
pillarone->collision_type = kRectCollisionType; 
cpCCSprite *pillar1 = [cpCCSprite spriteWithShape:pillarone file:@"004.png"]; 
[self addChild:pillar1]; 

pillar1.spaceManager = smgr; 

cpShape *pillartwo = [smgr addRectAt:cpv(500,300) mass:1 width:45 height:194 rotation:0]; 
pillartwo->collision_type = kRectCollisionType; 
cpCCSprite *pillar2 = [cpCCSprite spriteWithShape:pillartwo file:@"004.png"]; 
[self addChild:pillar2]; 

pillar2.spaceManager = smgr; 


cpShape *staticground = [smgr addRectAt:cpv(510,25) mass:1 width:0 height:0 rotation:0]; 
cpCCSprite *staticground1 = [cpCCSprite spriteWithShape:staticground file:@"grass1-1024.png"]; 
[self addChild:staticground1 z:1 tag:0]; 


[smgr addCollisionCallbackBetweenType:kRectCollisionType 
          otherType:kBallCollisionType 
           target:self 
          selector:@selector(handleCollisionWithFragmentingRect:arbiter:space:)]; 

代碼,這裏是用於衝突解決的代碼。

- (void) handleCollisionWithFragmentingRect:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space{ 
if (moment == COLLISION_POSTSOLVE) 
{ 
    [self removeChild:ball->data cleanup:YES]; 
    [smgr removeAndFreeShape:ball]; 

    ball = [smgr addCircleAt:cpv(1000,10) mass:0.5 radius:35]; 
    ball->collision_type = kBallCollisionType; 
    ballSprite = [cpCCSprite spriteWithShape:ball file:@"head.png"]; 
    [self addChild:ballSprite]; 
    ballSprite.spaceManager = smgr; 
} 

}

當第一球擊中消失罰款支柱。但是對於第二次,有時候我會選擇第三個球,並撞到它撞上的柱子,錯誤如下。

Chipmunk warning: Cannot remove a constraint that was not added to the space. (Removed twice maybe?) 
Failed condition: cpArrayContains(space->constraints, constraint) 

我不確定我哪裏出了問題,請誰能幫忙。

謝謝。

回答

0

我認爲你需要添加teh球以將其移除到「垃圾」數組,而不是在handleCollisionWithFragmentingRect函數中刪除它。對於所有的安全措施,包括

- (void) update : (ccTime) dt 
{ 
    for (cpShape *shape in _garbage) 
    { 
     // Remove shape 
     [self removeChild:shape->data cleanup:YES]; 
     [smgr removeAndFreeShape:shape]; 

     // Add new ball 
     ball = [smgr addCircleAt:cpv(1000,10) mass:0.5 radius:35]; 
     ball->collision_type = kBallCollisionType; 
     ballSprite = [cpCCSprite spriteWithShape:ball file:@"head.png"]; 
     [self addChild:ballSprite]; 
     ballSprite.spaceManager = smgr; 
    } 
    ... 

} 

然後你會改變手柄功能,這一點:

- (void) handleCollisionWithFragmentingRect:(CollisionMoment)moment arbiter:(cpArbiter*)arb space:(cpSpace*)space 
{ 
    if (moment == COLLISION_POSTSOLVE) 
    { 
     // SHOULDNT BE A DIRECT REFERENCE TO MEMBER VARIABLE BALL. 
     // SHOULD BE ADDED FROM INFORMATION WITHIN THE PARAMETERS OF THIS FUNCTION. 
     _garbage.addObject(ball); 
    } 
}