标签:des style blog class code java
很多天没有更新博客了,一方面是回杭州了几天,另一方面是开始做项目练习了。感谢关注我的一些朋友~
这些天会陆续把自己做这个项目的过程更新出来,喜欢的朋友可以一起学习一下
声明:转载请注明http://www.cnblogs.com/letougaozao/p/3708887.html
一、游戏该有的配置
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"icon.png"]];
这种方法需要将图片本身和控制器的View完全相同,不然就会平铺上去
@interface UIView (BgFull) //设置背景 - (void)setFullView:(NSString*)imageName; @end
- (void)setFullView:(NSString *)imageName { _imageName = imageName; //重画 [self setNeedsDisplay]; }
#pragma mark - 重画 - (void)drawRect:(CGRect)rect { UIImage *image = [UIImage imageNamed:_imageName]; if (!iPhone5) { //判断是iPhone4,剪图片 CGRect cutFrame = CGRectMake(0, 88, 640, 960); CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, cutFrame); image = [UIImage imageWithCGImage:imageRef]; } [image drawInRect:rect]; }
#define iPhone5 ([UIScreen mainScreen].bounds.size.height == 568)
- (void)viewDidLoad { [super viewDidLoad]; [self.view setFullView:@"home_bg.jpg"]; NSString *path = [[NSBundle mainBundle]pathForResource:@"home" ofType:@"plist"]; NSDictionary *dict1 = [NSDictionary dictionaryWithContentsOfFile:path]; _btnFrames = dict1[iPhone5 ? @"iphone5" : @"iphone4"]; btn_get_frame = CGRectFromString(_btnFrames[@"btn_get_frame"]); btn_language_frame = CGRectFromString(_btnFrames[@"btn_language_frame"]); btn_more_frame = CGRectFromString(_btnFrames[@"btn_more_frame"]); btn_play_frame = CGRectFromString(_btnFrames[@"btn_play_frame"]); btn_rank_frame = CGRectFromString(_btnFrames[@"btn_rank_frame"]); btn_setting_frame = CGRectFromString(_btnFrames[@"btn_setting_frame"]); }
UITouch *touch = [touches anyObject];
CGPoint hitPoint = [touch locationInView:self.view];
if (CGRectContainsPoint(btn_setting_frame, hitPoint)) { [[SoundManager sharedSoundManager] playSound:kClickSound]; [self performSegueWithIdentifier:@"setting" sender:nil]; } else if (CGRectContainsPoint(btn_play_frame, hitPoint)) { [[SoundManager sharedSoundManager] playSound:kClickSound]; [self performSegueWithIdentifier:@"stages" sender:nil]; }
[self.navigationController popViewControllerAnimated:NO];
#pragma mark -初始化方法 - (id)init { if (self = [super init]) { //加载音乐 [self loadMusic]; //加载音效 [self loadSound]; } return self; }
- (void)loadMusic { //3.1解档并且给静音设置状态 _musicMuted = [[NSUserDefaults standardUserDefaults] boolForKey:kMusicMuted]; NSURL *url = [[NSBundle mainBundle]URLForResource:@"bg_music.mp3" withExtension:nil]; _btMusicPlay = [[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil]; [_btMusicPlay prepareToPlay]; [_btMusicPlay setNumberOfLoops:-1]; }
- (void)playMusic { //4.2如果静音状态,直接返回 if (_musicMuted) return; [_btMusicPlay play]; }
- (void)loadSound { //3.2解档并且给静音设置状态 _soundMuted = [[NSUserDefaults standardUserDefaults] boolForKey:kSoundMuted]; _soundIDs = [NSMutableDictionary dictionary]; NSURL *bundleUrl = [[NSBundle mainBundle]URLForResource:@"sounds" withExtension:@"bundle"]; NSBundle *soundsBundle = [NSBundle bundleWithURL:bundleUrl]; //获得所有的URl NSArray *urlArray = [soundsBundle URLsForResourcesWithExtension:@"mp3" subdirectory:nil]; for (NSURL *url in urlArray) { SystemSoundID soundID; AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundID); NSString *fileName = [url.path lastPathComponent]; [_soundIDs setObject:@(soundID) forKey:fileName]; } }
- (void)playSound:(NSString*)soundName { //4.1如果静音状态,直接返回 if (_soundMuted) return; AudioServicesPlaySystemSound([_soundIDs[soundName]unsignedLongValue]); }
#define singleton_interface(className) + (className*)shared##className; #define singeton_implementation(className) static className *_instance; + (id)allocWithZone:(struct _NSZone *)zone { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [super allocWithZone:zone]; }); return _instance; } + (className*)shared##className { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[self alloc] init]; }); return _instance; }
2.在interface中声明
singleton_interface(SoundManager)
3.在implementation中实现
singeton_implementation(SoundManager)
//1.1设置属性 @property (nonatomic, assign) BOOL musicMuted; //1.2设置属性 @property (nonatomic, assign) BOOL soundMuted;
#pragma mark - 重写两个BOOL属性的set方法 - (void)setMusicMuted:(BOOL)musicMuted { //2.1保存状态,切归档 _musicMuted = musicMuted; [[NSUserDefaults standardUserDefaults] setBool:_musicMuted forKey:kMusicMuted]; [[NSUserDefaults standardUserDefaults] synchronize]; if (_musicMuted) { [_btMusicPlay pause]; } else { [_btMusicPlay play]; } } - (void)setSoundMuted:(BOOL)soundMuted { //2.2保存状态,切归档 _soundMuted = soundMuted; [[NSUserDefaults standardUserDefaults] setBool:_soundMuted forKey:kSoundMuted]; [[NSUserDefaults standardUserDefaults] synchronize]; }
_musicMuted = [[NSUserDefaults standardUserDefaults] boolForKey:kMusicMuted];
_soundMuted = [[NSUserDefaults standardUserDefaults] boolForKey:kSoundMuted];
5.根据状态,设置当中的文字,显示合适的值
- (IBAction)musicSwitch:(UIButton *)sender { //5.1这里判断音乐开关,切改变状态 SoundManager *sound = [SoundManager sharedSoundManager]; [sound playSound:kClickSound]; int music = sound.musicMuted; [sender setTitle:music ? @"音乐 [开]" : @"音乐 [关]" forState:UIControlStateNormal]; sound.musicMuted = !music; }
- (IBAction)soundSwitch:(UIButton *)sender { //5.2这里判断音乐开关,切改变状态 SoundManager *sound = [SoundManager sharedSoundManager]; int sounds = sound.soundMuted; [sender setTitle:sounds ? @"音效 [开]" : @"音效 [关]" forState:UIControlStateNormal]; sound.soundMuted = !sounds; [sound playSound:kClickSound]; }
#pragma mark -播放音效 - (void)playSound:(NSString*)soundName { //4.1如果静音状态,直接返回 if (_soundMuted) return; AudioServicesPlaySystemSound([_soundIDs[soundName]unsignedLongValue]); } - (void)playMusic { //4.2如果静音状态,直接返回 if (_musicMuted) return; [_btMusicPlay play]; }
应用程序开发之模仿史上最牛游戏(一),布布扣,bubuko.com
标签:des style blog class code java
原文地址:http://www.cnblogs.com/letougaozao/p/3708887.html