【问题标题】:textureImageData in plist? - Cocos 2D V 3.4 (CCParticleSystem)plist中的textureImageData? - Cocos 2D V 3.4 (CCParticleSystem)
【发布时间】:2016-01-06 13:22:30
【问题描述】:

我目前正在为新的 V 3.4 引擎修复一个旧的 Cocos iPhone Xcode 项目。它曾经工作得很好,但是我发现了这行代码:

CCParticleSystem *fire = [CCParticleSystemQuad particleWithFile:@"fire.plist"];

CCParticleSystemQuad 已被移除,因此我将其替换为“CCParticleSystem”:

CCParticleSystem *fire = [CCParticleSystem particleWithFile:@"fire.plist"];

它一直在该行代码处崩溃并显示以下错误消息:

- cocos2d: Couldn't find file:user_particle30956.png
- *** Assertion failure in -[CCTextureCache addCGImage:forKey:], /Users/.../Source/libs/cocos2d-iphone/cocos2d/CCTextureCache.m:370
- *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'

图像不存在,实际上作为纹理图像数据嵌入到 plist 文件中。我知道在 Cocos V 2 中,它会自动创建并存储此纹理图像数据作为带有名称的图像文件。 我怎样才能使这项工作适用于 V 3?

我不确定您需要多少有关 plist 文件的信息。这是一长串粒子系统配置(角度、持续时间、发射器类型等)之后的信息:

<key>textureFileName</key>
<string>user_particle30956.png</string>
<key>textureImageData</key>
<string>eJztnQecXWURxXdDgCCggBQLuisqKlbslcSCBcEC9pa1996wkbUr9q5...</string>

谢谢大家。

【问题讨论】:

    标签: objective-c cocos2d-iphone plist


    【解决方案1】:

    我也遇到了同样的问题。从particle designer docs 来看,嵌入的纹理显然是 TIFF,而不是 Cocos2D 所期望的 PNG。

    我不明白这一点,因为它曾经与 Cocos2D v2.2 一起工作得很好。

    在 v3.4.x 中,我所做的是将粒子设计器项目更改为将纹理导出为文件,然后将该文件添加到 Xcode 项目中,以便粒子系统以硬方式加载它。

    您也许可以查看 v2.2 代码以了解它的作用。也许该代码可以带入 v3.4.x

    [更新]查看 v2.2 代码后,从嵌入图像获取二进制数据并将其转换为纹理的机制使用 UIImage 而不是某些 PNG/JPEG 特定代码。

    // Cocos2d v2.2
    //
    #ifdef __CC_PLATFORM_IOS
                UIImage *image = [[UIImage alloc] initWithData:data];
    #elif defined(__CC_PLATFORM_MAC)
                NSBitmapImageRep *image = [[NSBitmapImageRep alloc] initWithData:data];
    #endif
    

    v3.x 代码执行此操作(适用于 iOS 和 Android):

    // Cocos2D v3.x
    //
    BOOL png = [[[dictionary valueForKey:@"textureFileName"] lowercaseString] hasSuffix:@".png"];
    CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
    CGImageRef image = (png) ? CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault) : CGImageCreateWithJPEGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault) ;
    

    我强烈怀疑以下方法可能有效(尽管我自己还没有测试过):

    #if __CC_PLATFORM_IOS
        UIImage *image = [[UIImage alloc] initWithData:data];
    
        [self setTexture:  [ [CCTextureCache sharedTextureCache] addCGImage:[image CGImage] forKey:textureName]];
    
    #elif __CC_PLATFORM_ANDROID
        BOOL png = [[[dictionary valueForKey:@"textureFileName"] lowercaseString] hasSuffix:@".png"];
        CGDataProviderRef imgDataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
        CGImageRef image = (png) ? CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault) : CGImageCreateWithJPEGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault) ;
    
        [self setTexture:  [ [CCTextureCache sharedTextureCache] addCGImage:image forKey:textureName]];
    
        CGDataProviderRelease(imgDataProvider);
        CGImageRelease(image);
    #elif __CC_PLATFORM_MAC
        NSBitmapImageRep *image = [[NSBitmapImageRep alloc] initWithData:data];
        [self setTexture:  [ [CCTextureCache sharedTextureCache] addCGImage:[image CGImage] forKey:textureName]];
    #endif
    

    我尝试在这里允许 Android,但如果嵌入的纹理确实是 TIFF,我看不出它会如何工作,并且 Android 桥接库也需要 PNG/JPEG。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 2011-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多