Cocos2D编程Xcode读取文件代码及其注释

-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
//step1:创建并获得一个指定文件路径test
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"Documents文件路径:%@",documentsDirectory);
NSFileManager *fileManage = [NSFileManager defaultManager];
NSString *myDiretory = [documentsDirectory stringByAppendingPathComponent:@"test"];
BOOL ok = [fileManage createDirectoryAtPath:myDiretory attributes:nil];
NSLog(@"test文件路径:%@",myDiretory);
//step2:设置需要读取的文件的绝对路径
NSMutableString *myfilePath = [NSMutableString stringWithCapacity:100];
[myfilePath appendString:myDiretory];
[myfilePath appendFormat:@"/player2.png"];//Three.txt"];
NSLog(@"myfilePath文件路径:%@",myfilePath);
//step3:读取在test下的一个不在压缩包的图片文件 <路径怎么写:这里将myDiretory进行字符串的转换然后将其进行加减就可以了>
NSData *data = [fileManage contentsAtPath:myfilePath];
NSLog(@"文件内容二进制形式:%@",data);
//step4:将这个不在包内图片内容进行显示
//这里已经将图片给剪切了
CCSprite *sprite = [CCSprite spriteWithFile:myfilePath rect:CGRectMake(0, 0, 28, 28)];
sprite.anchorPoint = ccp(0, 0);
[self addChild:sprite z:0];
}
return self;
}

以上代码详细的对怎样读取一个资源文件做了介绍,希望对大家有帮助。

原文地址:https://www.cnblogs.com/chenxiangxi/p/2279823.html