Cocos2d:如何检查Sprite Frame Name是否可用?

时间:2013-12-10 07:13:11

标签: ios objective-c cocos2d-iphone

我是Cocos2d框架的新手。

我正在尝试使用图片名称CCSpritesharedSpriteFrameCache创建// In init method [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"imgFile.plist"]; // In separate method CCSprite *spBubble = [CCSprite spriteWithSpriteFrameName:@"img1.png"];

spBubble = [CCSprite spriteWithSpriteFrameName:@"img1.png"];

现在有时它会在if(![CCSprite spriteWithSpriteFrameName:@"img1.png"]) { } 崩溃 因为它无法加载图像。

崩溃日志:

  

cocos2d:CCSpriteFrameCache:找不到框架'img1.png'

所以我试图通过

检查img1.png是否可用
if

但它也在[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"imgFile.plist"];条件下崩溃了。

那么如何检查img1.png是否可用?

如果没有,那么我可以再次加载 {{1}}

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。

CCSprite.m中,+(id)spriteWithSpriteFrameName:(NSString*)spriteFrameName方法CCSpriteFrame已创建。如果找不到图像,则返回的帧为nil。所以我直接使用该代码来检查帧是否为零。

CCSpriteFrame *frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"img1.png"];    
if(!frame)
{     
    // load image from .plist   
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"imgFile.plist"];
}  

这个技巧对我有用。