私はcocos2dのSimpleAudioEngineの機能をいくつかの種類の連鎖として次々と再生する機能を拡張しようとしています。私は拡張機能でこれをしようとしました。しかし、私は今、おそらくすべてのサウンドファイルの名前を覚えておくためにiVarと、現在再生中のサウンドを覚えておく必要があることに気付きました。Objective-Cカテゴリと新しいiVar
しかし、カテゴリに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
は、代替的に、私がコンパイルされます特性としてアイバーズを定義しようとしました。しかし、私はそれらを合成することも、他の方法にバインドすることもできません。
SimpleAudioEngineのカテゴリとして機能を実装しようとするので、すべてのサウンドの問題を扱うクラスを1つだけ覚えておく必要があります。そして私は、このような単純なチェーンを作成することができます:
[[SimpleAudioEngine sharedEngine] playSoundChainWithFileNames:@"6a_loose1D.mp3", @"6a_loose2D.mp3", @"6a_loose3D.mp3", @"6a_loose4D.mp3", @"6b_won1D.mp3", nil];
を、私も非常に感謝するのと同じ/同様の結果が得られ、別の方法がある場合。
なぜSimpleAudioEngineをサブクラス化しないのですか? – lawicko
[Objective-C:Categoryのプロパティ]の複製が可能です(http:// stackoverflow。com/questions/8733104/objective-c-property-in-category) – hfossli