2012-05-31 55 views
0

我是cocos2d和im的初學者,面臨檢測我的硬幣碰撞的問題。 有時候它有時候不起作用。Cocos2D - 檢測碰撞

所以基本上,即時通訊創建一個遊戲,用戶(船)必須避開障礙物並在途中收集硬幣。障礙物的碰撞效果很好,但不適用於硬幣。 我在想也許創造很多硬幣的循環是問題,但我不知道。

任何人都可以幫忙嗎?

我的代碼:

- (void)update:(ccTime)dt{ 
double curTime = CACurrentMediaTime(); 
if (curTime > _nextBridgeSpawn) { 

    float randSecs = [self randomValueBetween:3.0 andValue:5.0]; 
    _nextBridgeSpawn = randSecs + curTime; 

    float randX = [self randomValueBetween:50 andValue:500]; 
    float randDuration = [self randomValueBetween:8.0 andValue:10.0]; 

    CCSprite *bridge = [_bridge objectAtIndex:_nextBridge]; 

    _nextBridge++; 

    if (_nextBridge >= _bridge.count) _nextBridge = 0; 

    [bridge stopAllActions]; 
    bridge.position = ccp(winSize.width/2, winSize.height); 
    bridge.visible = YES; 

    [bridge runAction:[CCSequence actions: 
         [CCMoveBy actionWithDuration:randDuration position:ccp(0, -winSize.height)], 
         [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)], 
         nil]]; 

這就是我宣佈我的硬幣(從更新方法續)

int randCoin = [self randomValueBetween:0 andValue:5]; 
    _coin = [[CCArray alloc] initWithCapacity:randCoin]; 
    for(int i = 0; i < randCoin; ++i) { 
     coin = [CCSprite spriteWithFile:@"coin.png"]; 
     coin.visible = NO; 
     [self addChild:coin]; 
     [_coin addObject:coin]; 
    } 

    float randCoinX = [self randomValueBetween:winSize.width/5 andValue:winSize.width - (border.contentSize.width *2)]; 
    float randCoinY = [self randomValueBetween:100 andValue:700]; 
    float randCoinPlace = [self randomValueBetween:30 andValue:60]; 
    for (int i = 0; i < _coin.count; ++i) { 
     CCSprite *coin2 = [_coin objectAtIndex:i]; 
     coin2.position = ccp(randCoinX, (bridge.position.y + randCoinY) + (randCoinPlace *i)); 
     coin2.visible = YES; 
     [coin2 runAction:[CCSequence actions: 
          [CCMoveBy actionWithDuration:randDuration position:ccp(0, -winSize.height-2000)], 
          [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)], 
          nil]]; 
    } 

} 

這是檢查碰撞(也在更新方法)

for (CCSprite *bridge in _bridge) {   
    if (!bridge.visible) continue; 

if (CGRectIntersectsRect(ship.boundingBox, bridge.boundingBox)){ 
      bridge.visible = NO; 
      [ship runAction:[CCBlink actionWithDuration:1.0 blinks:5]];   

     } 
    } 
} 

//this is the collision for coins which only work at times 
    for (CCSprite *coin2 in _coin) { 

     if (!coin2.visible) continue; 

     if (CGRectIntersectsRect(ship.boundingBox, coin2.boundingBox)) { 
      NSLog(@"Coin collected"); 
      coin2.visible = NO; 
     } 

    } 

}

謝謝。

回答

0

手機在最後一段代碼是什麼?你在那裏檢查它是否與boundingBox相交硬幣,但我無法在其他地方看到它。另一種情況是,只有當你的對象有相同的父對象時,boundingBox才能工作,因爲它返回「本地」矩形,並與它的父對象相關

另一個要點是,如果對象的移動速度過快,他們會在嘀嗒聲時間內互相拋出對方。我的意思是,一個更新將在碰撞之前被調用,所以它不會檢測到它,下一個將在碰撞完成後被調用,所以它也不會檢測到它。

+0

喜,對不起這是一個錯字。它實際上ship.boundingBox – Grace

0

爲什麼你只是不檢查距離?

float dist = ccpDistance(ship.position, coin2.position); 
float allowedDist = ship.contentSize.width*0.5 + coin2.contentSize.width*0.5; 
if (dist<allowedDist){ 
    NSLog(@"Collision detected"); 
} 

如果你的精靈不在同一個圖層上,這些圖層應該至少有相同的位置。

+0

嗨,感謝您的答覆。我已經試過你的方法,我得到了與usi相同的結果邊界框。它可以感知一些與一些硬幣的碰撞,但有些不起作用。我在想,問題在於我用來創造硬幣的方法。 – Grace

0

我已經通過在init中創建一個計時器解決了我的問題,並每10秒調用一次來創建我的硬幣。

我的代碼:

-(void) createCoins:(ccTime)delta{ 

CGSize winSize = [CCDirector sharedDirector].winSize; 

int randCoin = [self randomValueBetween:0 andValue:10]; 
_coin = [[CCArray alloc] initWithCapacity:randCoin]; 
for(int i = 0; i < randCoin; i++) { 
    coin = [CCSprite spriteWithFile:@"coin30p.png"]; 
    coin.visible = NO; 
    [self addChild:coin]; 
    [_coin addObject:coin]; 
} 
float randCoinX = [self randomValueBetween:winSize.width/5 andValue:winSize.width - (border.contentSize.width *2)]; 
float randCoinY = [self randomValueBetween:100 andValue:700]; 
float randCoinPlace = [self randomValueBetween:30 andValue:60]; 
for (int i = 0; i < _coin.count; i++) { 
    CCSprite *coin2 = [_coin objectAtIndex:i]; 
    coin2.position = ccp(randCoinX, (winSize.height + randCoinY) + (randCoinPlace *i)); 
    coin2.visible = YES; 

    [coin2 runAction:[CCSequence actions: 
         [CCMoveBy actionWithDuration:8 position:ccp(0, -winSize.height-2000)], 
         [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible:)], 
         nil]]; 
} 

}

的更新方法

for (CCSprite *coins in _coin){ 
    if (!coins.visible) continue; 
    if (CGRectIntersectsRect(phone.boundingBox, coins.boundingBox)) { 
     NSLog(@"Coin collected"); 
     coins.visible = NO; 
    } 

} 

謝謝大家的幫助(: