2011-10-21 35 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对于根据其类型实际创建气球的建议仍然适用。

相关问题