iphone小技巧

 

让CCLayer响应ccTouchBegan而非Touches

In issue 652, a project member said:
Layer need to implement both methods: ccTouches and ccTouch. The default one is ccTouches but you can modify the default behavior by overriding  the "registerWithTouchDispatcher".

具体做法就是在CCLayer子类中增加如下代码:
   
   
   
   
view source print ?
-(void) registerWithTouchDispatcher {
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

 

在设备中新建并写入plist

关卡编辑器保存关卡数据时遇到的小问题,在iPad上跑总是无法读取到,原因是需要写到Document中而不是Resource中。

下面是一个Helper方法,用来生成可写路径:

 

         
         
         
         
+ (NSString * ) getLocalFilePath :(NSString * )fileName {
NSArray
* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString
* documentsDirectory = [paths objectAtIndex: 0 ];
return [documentsDirectory stringByAppendingPathComponent:fileName];
}

 

ios获取文件列表并按扩展名过滤

        
        
        
        
NSArray * levelList = [[[[NSFileManager alloc] init]
contentsOfDirectoryAtPath:[NSHomeDirectory() stringByAppendingPathComponent:
@" Documents " ]
error:nil]
pathsMatchingExtensions:[NSArray arrayWithObject:
@" plist " ]];
// 这里只是Documents目录的

iOS读Resource中的plist

NSString *cfgPath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@Cfg.plist", themeName] ofType:nil inDirectory:nil];
NSDictionary
*cfgDictionary = [NSDictionary dictionaryWithContentsOfFile:cfgPath];

你可能感兴趣的:(ios,移动开发,职场,iPhone,休闲)