2011-10-21 16 views
1

私はcocos2dを初めて使用しています。私のゲームでは、異なる色のバルーンがを無作為にに産まれていて、同じ(2つ以上の)スプライト/バルーンに関連付けられた特定のサウンドがあります。clickは動作しません。例えば。バルーンイメージred.pngではred.wavが関連付けられ、blue.png、blue.wavなどがそこにあります。再び赤いバルーンが来ると、red.wavがそれに関連付けられます。2つ以上の(同じ)スプライトで同様のサウンドを得るにはどうすればいいですか?

  • (ボイド)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:(ターゲットでCCSprite *スプライト)のために(するCGPoint)touchLocation {

    {

    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

私がしなければならなかったのは、2つ以上の同様のバルーンクリックで一意のサウンドを生成することでした。実際には文字列やすべてを作成する必要はありませんでした。単にタグと風船を関連付けるだけです。 –

+0

同じ出力をコーディングする方法はたくさんあります。上記の方法はすべて同じように優れています。しかし、あなた自身で解決策を見つける方が常に良いです!! – samfisher

+0

これは本質的に私のやり方です。enumはリストの最後に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]; 

次のようにpopメソッドが実装されています

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

    [[SimpleAudioEngine sharedEngine] playEffect:soundFile]; 
} 

したがってバルーンは、自己になりますバルーンは初期化中に、場合によってはプロパティを介してパラメータ化することができます。

実際にその種類に基づいて風船を作成するためのBongehの提案は依然として適用されます。私は私の問題への解決策を持っているし、それは美しく働いた

関連する問題