我尝试扩展cocos2d的SimpleAudioEngine的功能,能够一个接一个地播放几种音效作为某种链.我尝试使用扩展程序执行此操作.但是我现在意识到我可能还需要一个iVar来记住所有声音文件的名称,还有一个要记住当前播放的声音.
但似乎我无法在类别中添加iVars.相反,我尝试使用扩展,但似乎它们需要在类的原始.m文件中,这样也无法工作.还有另一种方式,允许我这样做吗?
带有类别的标题
#import <Foundation/Foundation.h>
@interface SimpleAudioEngine(SoundChainHelper)<CDLongAudioSourceDelegate>
-(void)playSoundChainWithFileNames:(NSString*) filename, ...;
@end
Run Code Online (Sandbox Code Playgroud)
和.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 …Run Code Online (Sandbox Code Playgroud)