2011-10-21 34 views
1

我是新來的cocos2d。在我的遊戲中,不同顏色的氣球從底部隨機產生,並且存在與類似(兩個或更多)精靈/氣球相關的特定聲音點擊這不起作用。例如。與氣球圖像red.png關聯red.wav和blue.png,blue.wav在那裏等等。再次當紅色氣球來臨時,red.wav與它相關聯。以下是我使用的音響協會代碼: -如何獲得兩個或更多(相同)精靈點擊類似的聲音?

  • (無效)selectSpriteForTouch:(CGPoint)touchLocation { 爲(CCSprite *在目標精靈) {

    if (CGRectContainsPoint([sprite boundingBox], touchLocation)) 
    
    { 
        //NSLog(@"sprite was touched"); 
        NSLog(@"strGetImgName%@",strGetImgName); 
        [targets removeObject:sprite]; 
        if ([strGetImgName isEqualToString:@"balloon1"]) { 
         [[SimpleAudioEngine sharedEngine] playEffect:@"button1.wav"]; 
         [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
        } 
        else if ([strGetImgName isEqualToString:@"balloon2"]) { 
         [[SimpleAudioEngine sharedEngine] playEffect:@"button2.wav"]; 
         [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
        } 
        else if ([strGetImgName isEqualToString:@"balloon3"]) { 
         [[SimpleAudioEngine sharedEngine] playEffect:@"button3.wav"]; 
         [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
        } 
        else if ([strGetImgName isEqualToString:@"balloon4"]) { 
         [[SimpleAudioEngine sharedEngine] playEffect:@"button4.wav"]; 
         [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
        } 
        else if ([strGetImgName isEqualToString:@"balloon5"]) { 
         [[SimpleAudioEngine sharedEngine] playEffect:@"button5.wav"]; 
         [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
        } 
        else if ([strGetImgName isEqualToString:@"balloon6"]) { 
         [[SimpleAudioEngine sharedEngine] playEffect:@"button6.wav"]; 
         [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
        } 
        else if ([strGetImgName isEqualToString:@"balloon7"]) { 
         [[SimpleAudioEngine sharedEngine] playEffect:@"button7.wav"]; 
         [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
        } 
        else if ([strGetImgName isEqualToString:@"balloon8"]) { 
         [[SimpleAudioEngine sharedEngine] playEffect:@"button8.wav"]; 
         [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
        } 
        else if ([strGetImgName isEqualToString:@"balloon9"]) { 
         [[SimpleAudioEngine sharedEngine] playEffect:@"button9.wav"]; 
         [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
        } 
        else if ([strGetImgName isEqualToString:@"balloon10"]) { 
         [[SimpleAudioEngine sharedEngine] playEffect:@"button10.wav"]; 
         [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
        } 
        else { 
         NSLog(@"nothing remaining"); 
        } 
    
        [self balloonBlastAnimation:sprite]; 
    
    
        [sprite.parent removeChild:sprite cleanup:YES]; 
    
    
        break; 
    
    } 
    

    } }

回答

0

我已經得到了解決我的問題,它精美的工作:)

//我已經爲每個氣球標籤和使用的開關情況的方法。這是我使用的代碼。

- (void)addTarget int enType = arc4random()%11;

CCSprite *target=[CCSprite spriteWithFile:[NSString stringWithFormat:@"balloon%d.png",enType] rect:CGRectMake(0, 0, 100, 119)]; 
target.tag = enType; 

//和開關殼體方法

  • (無效)selectSpriteForTouch:(CGPoint)touchLocation {

    爲(CCSprite *在目標子畫面) {

    if (CGRectContainsPoint([sprite boundingBox], touchLocation)) 
    
    { 
        switch (sprite.tag) { 
         case 1: 
          [[SimpleAudioEngine sharedEngine] playEffect:@"button1.wav"]; 
          [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
          break; 
          case 2: 
          [[SimpleAudioEngine sharedEngine] playEffect:@"button2.wav"]; 
          [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
          break; 
         case 3: 
          [[SimpleAudioEngine sharedEngine] playEffect:@"button3.wav"]; 
          [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
          break; 
         case 4: 
          [[SimpleAudioEngine sharedEngine] playEffect:@"button4.wav"]; 
          [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
          break; 
         case 5: 
          [[SimpleAudioEngine sharedEngine] playEffect:@"button5.wav"]; 
          [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
          break; 
         case 6: 
          [[SimpleAudioEngine sharedEngine] playEffect:@"button6.wav"]; 
          [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
          break; 
         case 7: 
          [[SimpleAudioEngine sharedEngine] playEffect:@"button7.wav"]; 
          [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
          break; 
         case 8: 
          [[SimpleAudioEngine sharedEngine] playEffect:@"button8.wav"]; 
          [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
          break; 
         case 9: 
          [[SimpleAudioEngine sharedEngine] playEffect:@"button9.wav"]; 
          [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
          break; 
         case 10: 
          [[SimpleAudioEngine sharedEngine] playEffect:@"button10.wav"]; 
          [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
          break; 
         default: 
          break; 
        } 
    
+0

我只需要在兩個或更多類似的氣球點擊上生成獨特的聲音。實際上不需要創建字符串和全部。簡單地將氣球與標籤關聯即可。 –

+0

有很多方法可以編碼相同的輸出。上述所有方法都同樣好。然而,自己找到解決方案總是更好...... !! – samfisher

+0

這實際上是我做這件事的方式,因爲枚舉只是給定值0的列表 - 列表的結尾。很高興你能解決這個問題。 – Bongeh

2

我會用一個枚舉要做到這一點,它更便宜,可以用一個簡單的做開關。

聲明你的枚舉

typedef enum { 
    BalloonType_1, 
    BalloonType_2, 
    BalloonType_3 
} BalloonType; 

子類CCSprite - 也許BalloonSprite,給它一個枚舉作爲一個屬性

@interface BalloonSprite : CCSprite { 

    BalloonType typeOfBalloon; 

} 

@synthesis的屬性,或使自己的方法聲明用於獲取和設置(我這樣做是因爲當我設置我的對象類型時,我在精靈內設置了健康/裝甲其他值)

-(BalloonType) typeOfBalloon; 
-(void) setTypeOfBalloon:(BalloonType) type; 

創建精靈時,請設置它的氣球類型。

然後你玩你的wav文件的方法看起來是這樣的..

switch ([sprite typeOfBalloon]) { 
    case: BalloonType_1 { 
     [[SimpleAudioEngine sharedEngine] playEffect:@"button1.wav"]; 
     [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
    } 
     break; 
    case: BalloonType_2 { 
     [[SimpleAudioEngine sharedEngine] playEffect:@"button2.wav"]; 
     [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
    } 
     break; 
    case: BalloonType_3 { 
     [[SimpleAudioEngine sharedEngine] playEffect:@"button3.wav"]; 
     [[SimpleAudioEngine sharedEngine] setEffectsVolume:4.0f]; 
    } 
     break; 
} 

希望有所幫助。

2

氣球應該是一個類,每個氣球是氣球類的一個實例。

當初始化氣球,你會然後做:

Balloon* newBalloon1 = [[Balloon alloc] initWithFile:@"balloon-red.png" sound:@"poppedipopp.wav"]; 
Balloon* newBalloon2 = [[Balloon alloc] initWithFile:@"balloon-blue.png" sound:@"pluppeddi.wav"]; 
Balloon* newBalloon3 = [[Balloon alloc] initWithFile:@"balloon-green.png" sound:@"flupffrrrrfrrr.wav"]; 

init方法做到這一點:

-(id) initWithFile:(NSString*)imageFile sound:(NSString*)sound 
{ 
    if ((self = [super init])) 
    { 
    sprite = [CCSprite spriteWithFile:imageFile]; 
    soundFile = sound; 
    } 
} 

的@interface聲明CCSprite *雪碧的NSString *音效檔實例變量。

當氣球應該彈出,你會發送彈出消息氣球:

[aBalloon pop]; 

方法作爲實現的彈出如下:

-(void) pop 
{ 
    // maybe play an animation 

    [[SimpleAudioEngine sharedEngine] playEffect:soundFile]; 
} 

因此氣球成爲自包含知道它們應該做什麼的對象,並且可以在初始化期間或可能通過屬性對氣球進行參數化。

Bongeh對於根據其類型實際創建氣球的建議仍然適用。

相關問題