如何在Cocos2d中使用NSArray播放随机声音

时间:2012-05-16 06:06:21

标签: audio random cocos2d-iphone nsarray

我试图在用户触摸按钮时进行随机声音播放。到目前为止,这是我的代码:

-(void) playEffect {    
    NSArray *array = [NSArray arrayWithObjects:@"1.mp3", "2.mp3", "3.mp3", nil];
    int index;
    index = random() % array.count;
    sound *theSound = [array objectAtIndex:index]; 
    [theSound play];    
}

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    //Blah, blah, blah

    //PLAY RANDOM SOUND
    [self playEffect];  
}

提前感谢您的时间和帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用CocosDenshion。像Smth一样

[[SimpleAudioEngine sharedEngine] playEffect: pathToYourSoundFile];