Gracias por la respuesta de @ adam.artajew! Lo modifiqué para usarlo con Cocos2D v3
e hice una categoría Crossfade
en el motor de audio para usarlo desde cualquier lugar del juego. Ahora que realiza las acciones siguientes:
- Fundido en
- Cambio pista
- Fundido en
En OALSimpleAudio+Crossfade.m
incluyen estas importaciones:
#import "CCDirector_Private.h"
#import "CCActionManager.h"
y poner en práctica el método:
- (void)playBg:(NSString *)name crossfade:(BOOL)crossfade {
// Skip if already playing requested track
if (self.bgPlaying &&
[self.backgroundTrack.currentlyLoadedUrl.lastPathComponent isEqualToString:name]) {
return;
}
// Play right now if no crossfade needed
if (!crossfade) {
[self playBg:name loop:true];
}
// Fade out just if music's playing right now
NSMutableArray *actions = [NSMutableArray array];
if (self.bgPlaying) {
id fadeOut = [CCActionTween actionWithDuration:0.5 key:@"bgVolume" from:self.bgVolume to:0.0f];
[actions addObject:fadeOut];
}
// Replace current track with fade in effect
id playNew = [CCActionCallBlock actionWithBlock:^{
[self playBg:name loop:true];
}];
id fadeIn = [CCActionTween actionWithDuration:0.5 key:@"bgVolume" from:0.0f to:1.0f];
// Combime final action
[actions addObjectsFromArray:@[playNew, fadeIn]];
id sequence = [CCActionSequence actionWithArray:actions.copy];
// Run action
[[[CCDirector sharedDirector] actionManager] addAction:sequence target:self paused:NO];
}
Uso: IncluirOALSimpleAudio+Crossfade.h
y llamar
[[OALSimpleAudio sharedInstance] playBg:@"MainBgMusic.mp3" crossfade:YES];
Muy bien, gracias. –