一次多个CCSpritevFramevCache

时间:2012-01-30 14:23:14

标签: cocos2d-iphone

我经常使用CCSpriteFrameCache,但无法理解它的相关信息。 我可以在游戏开始时将多个.plist加载到缓存中吗?或者CCSpriteFrameCache一次只有一个plist

到目前为止,有一个精灵,它是CCSpriteBatchNode的孩子,在游戏过程中已经多次创建,具有不同的图像。所以每次我创建一个新的精灵,我都会这样做:

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[NSString stringWithFormat:@"cand%i.plist",stage]];
     candySheet = [CCSpriteBatchNode batchNodeWithFile:[NSString stringWithFormat:@"cand%i.png",stage]];
[self addChild:candySheet];
sprite1 = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"cand%i.png",1]];

没关系?

现在假设我有2个spriteSheets和2个.plist,我想在我的init上加载它们,并添加2个精灵,每个精灵都是一个CCSpriteBatchNode的孩子并为每个精灵渲染图像来自他自己的spriteSheet和Plist的精灵。但那时我得到了错误:

CCSprite is not using the same texture id

所以,据我所知,每次我必须在特定时间加载缓存我需要的plist ????

感谢。

2 个答案:

答案 0 :(得分:1)

之前有人问过类似的事情。

如果它们不是来自同一个spritesheet,则不能为同一个CCSpriteBatchNode生成精灵。

您需要为您使用的每个spritesheet创建一个新的CCSpriteBatchNode(通过spritesheet我的意思是组合的图像文件和.plist文件)

CCSpriteFrameCache是​​在所有场景和类之间共享的单个缓存。当你调用这个方法时:

[CCSpriteFrameCache sharedSpriteFrameCache]

每次都没有创建一个新的CCSpriteFrameCache对象,只有一个实例。您将所有已加载的spritesheets存储在此单个缓存中。因此,您可以将2个spritesheets加载到缓存中,如下所示:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@“sheet1.plist”]; [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@“sheet2.plist”];

然后,您需要为EACH spritesheet创建一个CCSpriteBatchNode,批处理节点中不能有多个工作表:

CCSpriteBatchNode * spriteSheet1 = [CCSpriteBatchNode batchNodeWithFile:@“sheet1.pvr.ccz”];

CCSpriteBatchNode * spriteSheet2 = [CCSpriteBatchNode batchNodeWithFile:@“sheet2.pvr.ccz”];

如果愿意,您可以将这两个批处理节点添加到图层。添加到批处理节点的Sprite必须来自批处理节点正在使用的spritesheet。

答案 1 :(得分:0)

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"some.plist"];

如果精灵帧已经加载/缓存,上面的行需要花费相当多的时间甚至。所以一定要避免这样做,除非你绝对确定这些精灵帧之前没有被缓存过,比如在游戏开始时或者在清除精灵帧缓存之后。

相关问题