我想扩展cocos2d的SimpleAudioEngine功能,使其能够按照某种链式方式播放多个音效。我试图使用扩展来实现此功能。然而,我现在意识到我可能还需要一个iVar来记住所有声音文件的名称,并且需要一个iVar来记住当前正在播放的声音。
但是,似乎我不能在类别中添加iVars。我尝试使用扩展,但是它们似乎需要在类的原始.m文件中,因此这也行不通。是否有另一种方法可以让我做到这一点?
带类别的头文件
如果有其他能够产生相同/类似结果的方法,我也会非常感激。
但是,似乎我不能在类别中添加iVars。我尝试使用扩展,但是它们似乎需要在类的原始.m文件中,因此这也行不通。是否有另一种方法可以让我做到这一点?
带类别的头文件
#import <Foundation/Foundation.h>
@interface SimpleAudioEngine(SoundChainHelper)<CDLongAudioSourceDelegate>
-(void)playSoundChainWithFileNames:(NSString*) filename, ...;
@end
文件扩展名为 .m:
#import "SoundChainHelper.h"
@interface SimpleAudioEngine() {
NSMutableArray* soundsInChain;
int currentSound;
}
@end
@implementation SimpleAudioEngine(SoundChainHelper)
// read in all filenames and start off playing process
-(void)playSoundChainWithFileNames:(NSString*) filename, ... {
soundsInChain = [[NSMutableArray alloc] initWithCapacity:5];
va_list params;
va_start(params,filename);
while (filename) {
[soundsInChain addObject:filename];
filename = va_arg(params, NSString*);
}
va_end(params);
currentSound = 0;
[self cdAudioSourceDidFinishPlaying:nil];
}
// play first file, this will also always automatically be called as soon as the previous sound has finished playing
-(void)cdAudioSourceDidFinishPlaying:(CDLongAudioSource *)audioSource {
if ([soundsInChain count] > currentSound) {
CDLongAudioSource* mySound = [[CDAudioManager sharedManager] audioSourceForChannel:kASC_Right];
[mySound load:[soundsInChain objectAtIndex:0]];
mySound.delegate = self;
[mySound play];
currentSound++;
}
}
@end
另一种方法是将iVars定义为属性,这样可以编译。但是我既不能合成它们,也没有其他绑定它们到任何方法的可能性。
我尝试将功能实现为SimpleAudioEngine的类别,这样我只需要记住一个处理所有声音问题的类,以便我可以创建如下简单的链:
[[SimpleAudioEngine sharedEngine] playSoundChainWithFileNames:@"6a_loose1D.mp3", @"6a_loose2D.mp3", @"6a_loose3D.mp3", @"6a_loose4D.mp3", @"6b_won1D.mp3", nil];
如果有其他能够产生相同/类似结果的方法,我也会非常感激。