动画加载:图片存储在plist文件中,其中时按照数据字典存储的
//1.指定路径
NSString *path=[[NSBundlemainBundle]pathForResource:@"Tomcat"ofType:@"plist"];
//2.加载数据字典
NSMutableDictionary *_tomcatdict=[NSMutableDictionarydictionaryWithContentsOfFile:path];
//3.加载动画:
//frames存储图片个数
//imageFormat存储文件名为“aaa %04d”, 例如: aaa0001
NSMutableArray *imageList=[NSMutableArrayarray];
for (NSInteger i=0; i<[dict[@"frames"]integerValue]; i++) {
NSString *imageFile=[NSString stringWithFormat:dict[@"imageFormat"],i];
UIImage *image=[UIImage imageNamed:imageFile];
[imageList addObject:image];
//4.tomcat动画显示
//设置动画:动画图片+时间+重复次数+开始动画
[_tomcatImagesetAnimationImages:imageList];
[_tomcatImagesetAnimationDuration:[dict[@"frames"]integerValue]/10.0];
[_tomcatImagesetAnimationRepeatCount:1];
[_tomcatImagestartAnimating];
声音加载
1. 需要导入AVFoundation框架
2. 需要从Bundle中加载文件,播放之前需要使用preparePlay方法,准备播放
3. 可以设置声音的循环次数、音量大小
4. 播放效率相对略低,但是可以播放较大的声音文件
#pragma mark 初始化音乐播放器
-(AVAudioPlayer *)InitMusicPlayer
{
//初始化音乐播放器
//1.音乐路径
NSString *path=[[NSBundle mainBundle]pathForResource:@"BackgroudMusic" ofType:@"caf"];
//2.路径转为url,从本地用fileurlwithpath,从网络用url with path
NSURL *url=[NSURL fileURLWithPath:path];
//3.初始化
AVAudioPlayer * player=[[AVAudioPlayer alloc]initWithContentsOfURL:url error:nil];
//4.设置播放器属性
// 0: 播放一次, 1:播放2次, <0: 无限循环播放
[player setNumberOfLoops:-1];
//5.音乐播放前准备
[player prepareToPlay];
return player;
}
#pragma mark 加载音效
-(SystemSoundID)loadSound:(NSString *)soundFileName
{
//指定声音的文件路径,加载不同的音效
NSString *path=[[NSBundle mainBundle]pathForResource:soundFileName ofType:nil];
NSLog(@"path is %@",path);
NSURL *url=[NSURL fileURLWithPath:path];
NSLog(@"url is %@",url);
//初始化音效
//url->cfurlREF
SystemSoundID soundId;
//这是一个c语言框架
AudioServicesCreateSystemSoundID((__bridge CFURLRef)(url), &soundId);
return soundId;
}
//6.播放
_backMusicPlayer=[self InitMusicPlayer];
[_backMusicPlayer setVolume:0.5f];
[_backMusicPlayer play];
//初始化音效
_winSound=[self loadSound:@"Win.aiff"];
_failSound=[self loadSound:@"Fail.aiff"];
_drewSound=[self loadSound:@"Drew.aiff"];
_clickSound=[self loadSound:@"Click.aiff"];
1. 导入AudioToolBox框架
2. 使用C语言的函数直接将音频加载到内存,通过SystemSoundID进行播放
3. 在播放的时候,不容易中断,也控制不了音量
4. 播放效率高,通常使用不超过2秒的音频文件
5. 为了避免同一个音效文件在不同视图控制器中被重复加载,造成无谓的内存消耗,在使用音效时,通常会用一个数据字典进行维护
//音乐文件加载
-(SystemSoundID)loadSoundId:(NSString *)soundFile
{
NSString *path=[[NSBundlemainBundle]pathForResource:soundFile ofType:nil];
NSURL *url=[NSURL fileURLWithPath:path];
SystemSoundID soundId;
AudioServicesCreateSystemSoundID((__bridgeCFURLRef)(url), &soundId);
return soundId;
}
//初始化音效字典
NSMutableDictionary *_soundDict=[NSMutableDictionarydictionary];
/*---------------------------------------------------------------------------*/
//从数据字典中取出声音文件数组
NSArray *array=dict[@"soundFiles"];
//判断数组是否有数据,如果有进一步处理
SystemSoundID soundId=0;
if (array.count>0)
{
for (NSString *fileName in array) {
//转为数值类型
SystemSoundID playSoundId=[_soundDict[fileName]unsignedLongValue];
//如果在字典中没有定义音频代号,初始化音频id,并加入字典
if(playSoundId<=0)
{
playSoundId=[selfloadSoundId:fileName];
//将playsoundid加入数据字典,
//向字典中加入数据不能用add
//向nsdictionary, nsarray添加数值需要包装,
//@()把一个nsinteger的数字变为nsnumber的对象
[_soundDictsetValue:@(playSoundId)forKey:fileName];
}
}
NSLog(@"sounddict%@",_soundDict);
}
//每个动画的声音可以是多个,为了保证游戏的可玩度,可以采用随机数的方式播放音效
NSInteger seed=arc4random_uniform(array.count);
//数组根据序号,字典根据键值
NSString *fileName=array[seed];
soundId=[_soundDict[fileName]unsignedLongValue];
//播放音频
if (soundId>0) {
AudioServicesPlaySystemSound(soundId);
}