Cảm ơn câu trả lời của @ adam.artajew! Tôi đã sửa đổi nó để sử dụng với Cocos2D v3
và tạo một danh mục Crossfade
trên công cụ âm thanh để sử dụng nó từ bất kỳ nơi nào trong trò chơi. Bây giờ nó thực hiện những hành động sau đây:
- Fade ra
- Thay đổi theo dõi
- Fade in
Trong OALSimpleAudio+Crossfade.m
bao gồm các hàng nhập khẩu:
#import "CCDirector_Private.h"
#import "CCActionManager.h"
Và thực hiện phương pháp:
- (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];
}
Cách sử dụng: Bao gồmOALSimpleAudio+Crossfade.h
và gọi
[[OALSimpleAudio sharedInstance] playBg:@"MainBgMusic.mp3" crossfade:YES];
Được rồi, cảm ơn bạn. –