IOS cocos2d学习笔记-<八>游戏关卡(Objective-C文件读写)

来源:互联网 发布:android传递数据的方式 编辑:程序博客网 时间:2024/05/21 18:39

(本文写作基于千锋3g学院提供的第三季讲授视频编写,更多资料和资源下载地址请看笔记扉页:传送门

这一期的视频内容不多,涉及Objective-C的记本内容:文件读写

在代码里面,讲师写了一个专门用作文件读写的类-GameUtils

里面包括了两个类方法:

+ (int) readLevelFromFile

+ (void) writeLevelToFile:(int)level

这个两个类分别用作从文件种读取关卡信息将关卡信息写入文件

这种使用方法是一个非常棒的编程思想,游戏种任何需要用到文件读写的地方我们都可以直接调用这个类的方法来实现文件读写,

这是低耦合度的面向对象编程者所要具备的思想,我们学习学习之。

下面是这个类的实现代码:

GameUtils.m

#import "GameUtils.h"@implementation GameUtils- (NSString *) getLevelFilePath{    // 得到成功通过关卡的文件    return [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"SuccessLevel"];    // 存在沙盒 Documents/SuccessLevel;}+ (int) readLevelFromFile{    NSString *file = [[self class] getLevelFilePath];    // 获取文件路径    NSString *s = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];    if (s)    {        return [s intValue];    }    return 2;}+ (void) writeLevelToFile:(int)level{    NSString *s = [NSString stringWithFormat:@"%d", level];    // 把level转换成字符串    NSString *file = [[self class] getLevelFilePath];    // 取得要存放的文件    [s writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:nil];}@end


这个类里面有一个这样的方法

+ (NSString *) getLevelFilePath;

这是直接获取目标地址字符串的方法,我们知道,在读与写文档的时候,我们都要获取一个地址,分别写进方法里面会代码重复。

这样做就简洁很多了。


我在工程里面添加了一句

[GameUtilswriteLevelToFile:2];

写了一个文档进去目标地址中,并把+(NSString *) getLevelFilePath方法返回的地址打印了一下,沿着地址找到我们的文档,用文本编辑app打开,也确实只有一个‘2’字,好吧,正常使用。



好,你跑不掉的,就是你。

NSHomeDirectory()
别躲在那里以为我看不见你,你不就是用来获取当前应用的沙盒位置吗?

我再百度一下相关资料:

NSString *path = NSHomeDirectory();
上面的代码得到的是应用程序目录的路径,在该目录下有三个文件夹:Documents、Library、temp以及一个.app包!
该目录下就是应用程序的沙盒,应用程序只能访问该目录下的文件夹!!!

请参考下面的例子:
1、
NSString *path1 = NSHomeDirectory();

NSLog(@"path1:%@", path1);
path1:/Users/yuanjun/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830
2、
NSString *path2 = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

NSLog(@"path2:%@", path2);
path2:/Users/yuanjun/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830/Library/Caches
3、
NSString *path3 = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

NSLog(@"path3:%@", path3);
path3:/Users/yuanjun/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830/Documents
4、
NSString *path4 = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

NSLog(@"path4:%@", path4);
path4:/Users/yuanjun/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830/Documents
5、
NSString *path5 = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];

NSLog(@"path5:%@", path5);
path5:/Users/yuanjun/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830/Library
6、
NSString *path6 = [NSHomeDirectory() stringByAppendingPathComponent:@"temp"];

NSLog(@"path6:%@", path6);
path6:/Users/yuanjun/Library/Application Support/iPhone Simulator/4.2/Applications/172DB70A-145B-4575-A31E-D501AC6EA830/temp

(转自:http://www.gdcvi.com/?CH=Log&ArticleID=86399)

好,这下就全了。