1

你可能知道,Cocos2D-iphone 1.x 中 CCFileUtils 的所有类方法都被重写为 Cocos2d 2.x 中的单例 [CCFileUtils sharedFileUtils] 的实例方法,所以不是

[CCFileUtils fullPathFromRelativePath:... resolutionType:...]

我们需要使用

[[CCFileUtils sharedFileUtils] fullPathFromRelativePath:maskFileName resolutionType:&resolution]

我编写了一个用于屏蔽精灵的扩展(http://www.cocos2d-iphone.org/forum/topic/30494),它大量使用 CCFileUtils,现在需要使其与 cocos2d 1.x 和 2.x 兼容

我知道 CC_ENABLE_DEPRECATED 标志,它有助于即时翻译这些调用,但我想有一个明确的解决方案。类似于类方法的东西,它将返回一个 id 以便像这样使用它:

Method:
+ (id) getCCFileUtils
{
id fileUtils = [CCFileUtils class];
if ([fileUtils instancesRespondToSelector:@selector(fullPathFromRelativePath:resolutionType:)])
{
    return [fileUtils sharedFileUtils];
}
else
{
    return fileUtils;
}
}
Usage:
id myFileUtils = [MyClass getCCFileUtils];

[myFileUtils fullPathFromRelativePath:maskFileName resolutionType:&resolution]

}

当然,我在 Cocos 1.x 中收到错误“No known instance method for selector 'sharedFileUtils'”,因为没有这样的选择器。我应该如何重写它才能开始工作?

4

1 回答 1

1

好吧,我睡了一会儿后自己做的:)分享代码:

/**In Cocos2d v 2.0 CCFileUtils class methods were moved to instance methods.
 Here we set SSK_FILE_UTILS to class or instance depending on Cocos2D version.
 */
#ifdef COCOS2D_VERSION
    #if COCOS2D_VERSION >= 0x00020000
        #define SSK_FILE_UTILS [CCFileUtils sharedFileUtils]
    #else 
        #define SSK_FILE_UTILS CCFileUtils
    #endif
#else
    #define SSK_FILE_UTILS nil
#endif

用法:

[SSK_FILE_UTILS fullPathFromRelativePath:... resolutionType:...]
于 2012-05-11T20:26:35.090 回答