每当用户触摸按钮时,我都会尝试随机播放声音。到目前为止,这是我的代码:
-(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];
}
预先感谢您的时间和协助。
最佳答案
您可以为此使用CocosDenshion。有点像
[[SimpleAudioEngine sharedEngine] playEffect: pathToYourSoundFile];
关于audio - 如何在Cocos2d中使用NSArray播放随机声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10612897/