RT,第一次播放正常,第二次触发开始就只播放短暂的闪音
程序结构是这样的:
加载和播放 Sound.m
第一次触发 Start.m
第二次触发 Menu.m
只是给按钮配音,下面贴代码
Sound.m
#import "Sound.h"
@implementation Sound
-(void) Button{
NSString *Path_Sound_Button_Start = NSBundle mainBundle] pathForResource:@"Button18" ofType:@"wav"];
NSURL *PathUrl_Sound_Button_Start = NSURL alloc] initFileURLWithPath:Path_Sound_Button_Start];
Sound_Button_Start = AVAudioPlayer alloc] initWithContentsOfURL:PathUrl_Sound_Button_Start error:nil];
Sound_Button_Start prepareToPlay];
Sound_Button_Start setVolume:1];
Sound_Button_Start.numberOfLoops = 1;
Sound_Button_Start play];
}
@end
Start.m
#import "Start.h"
#import "Home.h"
#import "Sound.h"
@implementation Start
+ (Start *)scene{
return self alloc] init];
}
#pragma mark - init
- (id)init{
self = super init];
if (!self) return(nil);
CGPoint Screen_Center = CGPointMake(self.contentSize.width/2, self.contentSize.height/2);
// Start button
CCButton *Button_Start = CCButton buttonWithTitle:@" Start ]" fontName:@"Verdana-Bold" fontSize:18.0f];
Button_Start.position = Screen_Center;
;
self addChild:Button_Start];
return self;
}
#pragma mark - Button Callbacks
- (void)onSpinningClicked:(id)sender{
Sound *sound = Sound alloc] init];
;
// To Home with transition
CCDirector sharedDirector] replaceScene:Home scene]
withTransition:CCTransition transitionRevealWithDirection:CCTransitionDirectionLeft duration:3.0f]];
}
@end
Menu.m
#import "Start.h"
#import "Home.h"
#import "Sound.h"
@implementation Home{
}
#pragma mark - Create & Destroy
+ (Home *)scene{
return self alloc] init];
}
- (id)init{
self = super init];if (!self) return(nil);
// Enable touch handling on scene node
self.userInteractionEnabled = YES;
CGPoint Screen_Center = CGPointMake(self.contentSize.width/2, self.contentSize.height/2);
// Create a colored background (Dark Grey)
CCNodeColor *background = CCNodeColor nodeWithColor:CCColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f]];self addChild:background];
// Button_Back
CCButton *Button_Back = CCButton buttonWithTitle:@" Back ]" fontName:@"Verdana-Bold" fontSize:18.0f];
Button_Back.position = ccp(self.contentSize.width-50, self.contentSize.height-20);
;
self addChild:Button_Back];
return self;
}
- (void)dealloc{
// clean up code goes here
}
#pragma mark - Enter & Exit
- (void)onEnter{
super onEnter];
}
- (void)onExit{
super onExit];
}
#pragma mark - Button Callbacks
- (void)onBackClicked:(id)sender{
Sound *sound = Sound alloc] init];
;
// To Start with transition
CCDirector sharedDirector] replaceScene:Start scene]
withTransition:CCTransition transitionRevealWithDirection:CCTransitionDirectionRight duration:0.25f]];
}
@end