2011-07-27 40 views
0

在我的簡單遊戲中,我使用boundingBox來拾取硬幣和其他東西,但我需要使用Sprite(不含Alpha)的不規則區域檢測。是否有替代boundingBox確定CCSprite的不規則區域

這裏是代碼:

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { 
CGPoint location = [touch locationInView:[touch view]]; 
CGPoint point = [[CCDirector sharedDirector] convertToGL:location]; 
curentPosition = point; 
arrToDel = [[NSMutableArray alloc] initWithCapacity:0]; 
// Находим спрайт под касанием 

if (CGRectContainsPoint([some boundingBox], curentPosition)) { 
    CCLOG(@"popal"); 
} 

CCSprite *coin = nil; 

for (Coins *coins in self.bugs) { 
    if (CGRectContainsPoint([coins boundingBox], curentPosition)) { 
     coin = coins; // нашли монету 
    } 
} 

if (coin != nil) { 
    NSMutableArray *checkList = [NSMutableArray arrayWithCapacity:0]; 

    for (Coins *coins in self.bugs) { 
     if (CGRectIntersectsRect([coin boundingBoxInPixels], [coins boundingBox]) && coins != coin) { 
      [checkList addObject:coins]; 
     } 
    } 

    int max = coin.zOrder; 

    for (Coins *b in checkList) { 
     if (b.zOrder > max) 
      max = b.zOrder; 
    } 

    if (max == coin.zOrder) { 

     [self removeChild:coin cleanup:YES]; 
     podsciot++; 
     CCLOG(@"%i",podsciot); 

     [arrToDel addObject:coin]; 

     for (Coins *coins in arrToDel) { 
      if (coins.type == kKey) { 
       coinsCount++; 
       CCLOG(@"SeriiZ --> %i", coinsCount); 
      } 

      [self.bugs removeObject:coin]; 

     } 
    } 
} 

回答

0

我個人建議的Box2D和PhysicsEditor如果你需要不規則形狀。它有一個輕微的學習曲線(Box2D),但對於大多數用途來說是非常值得的。

Box2D (although it comes with Cocos2D and has a template built in)

PhysicsEditor (not free, but I recommend it as the program is very easy to use and the developer is a kind man)

這不僅是物理模擬。如果你喜歡,你只能用它來進行碰撞檢測。雷Wenderlich的網站有隻是一個很好的教程:

Box2D For Just Collision Detection