【问题标题】:CCFileUtils: transition from class methods in Cocos2D-iphone 1.x to instance methods in Cocos2D 2.xCCFileUtils:从 Cocos2D-iphone 1.x 中的类方法过渡到 Cocos2D 2.x 中的实例方法
【发布时间】:2012-05-10 15:12:58
【问题描述】:

你可能知道,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'”,因为没有这样的选择器。我应该如何重写它才能开始工作?

【问题讨论】:

    标签: cocos2d-iphone


    【解决方案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-06-20
      • 1970-01-01
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多