Cocos2D 2.0 - 一个CCSpriteBatchNode可以用于多个类吗? [英] Cocos2D 2.0 - Can one CCSpriteBatchNode be used for multiple classes?

查看:141
本文介绍了Cocos2D 2.0 - 一个CCSpriteBatchNode可以用于多个类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这是否可能,但我想创建一个大纹理地图集,并在所有类的应用程序使用它。



CCSpriteBatchNode用于多个类?



假设我在主类上创建这个类

  [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@atlasGeral.plist]; 
self.batchNodeGeneral = [CCSpriteBatchNode batchNodeWithFile:@atlasGeral.png];
[self addChild:self.batchNodeGeneral];

我有另一个类在主类上创建CCLayers,在初始化之前,使用CCSpriteBatchNode这个:

   - (id)init 
{

if((self = super init])){

self.bg = [CCSprite spriteWithFile:@cCircularBG.png];
[self addChild:self.bg];

self.dr = [CCSprite spriteWithFile:@cCircularDR.png];
[self addChild:self.dr];
}
return self; // self is a CCLayer
}

可以使用self.batchNodeGeneral主类?我的想法是用 [CCSprite spriteWithSpriteFrameName:... ]替换这两个精灵和其他的精灵



感谢

解决方案

我不完全确定我会跟着,但我很肯定答案是肯定的。



CCSpriteBatchNode与类没有任何关系,它与资产有关。对批处理节点的使用的重要限制是批处理中的每个sprite需要引用相同的纹理图集。所以对于整个应用程序有一个批处理节点是完全正确的,并且每个游戏类都向该批添加自己的sprite。如果你的纹理图集大于你的目标硬件的最大纹理尺寸(详情参见iOS设备规范),这可能会变成一个实际问题,但如果你仍然想要全局批次和大量的资源,它不是太难创建按纹理ID索引的批处理池,每个atlas创建一个批处理节点,并且每当创建一个新的sprite时,将其添加到相应的批处理中。

老实说,我觉得整个批处理节点事情是一个可怕的kludge在Cocos2D的一部分,可以使得几乎完全透明的开发人员,而仍然保留其效率,但也许这个意见不是完全公平,因为我没有在渲染代码中了解他们的动机。我想这会混乱的期望,如何深度排序工作等,但仍然我不明白为什么批处理对象渲染是程序员的责任,它应该由引擎完成。



编辑以添加可能的解决方案:

   - (id)initWithMainClass:(MainClass *)mc 
{

if((self = [super init])){

self.bg = [CCSprite spriteWithSpriteFrameName:@cCircularBG.png];
[mc.batchNodeGeneral addChild:self.bg];

self.dr = [CCSprite spriteWithSpriteFrameName:@cCircularDR.png];
[mc.batchNodeGeneral addChild:self.dr];
}
return self; // self is a CCLayer
}
`

的其他类,您将主类实例作为参数传递,以便您可以访问其字段。或者让主/管理员类成为单例,或者找到适合您需要的另一个架构。


I don't know if this is possible, but I would like to create one big texture atlas and use it on all classes of the application.

Can one CCSpriteBatchNode be used for multiple classes?

Suppose I create this on the main class

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"atlasGeral.plist"];
self.batchNodeGeneral = [CCSpriteBatchNode batchNodeWithFile:@"atlasGeral.png"];
[self addChild:self.batchNodeGeneral];

and I have another class creating CCLayers on the main class, that is initialized, before using CCSpriteBatchNode, like this:

-(id) init 
{

   if( (self=[super init])) {

        self.bg = [CCSprite spriteWithFile: @"cCircularBG.png"];
        [self addChild:self.bg];

        self.dr = [CCSprite spriteWithFile: @"cCircularDR.png"];
        [self addChild:self.dr];      
  }
  return self;  // self is a CCLayer
}

can this be optimized using the self.batchNodeGeneral from the main class? My idea is to replace these two sprites and others with something like [CCSprite spriteWithSpriteFrameName:...

thanks

解决方案

I'm not entirely sure I follow, but I'm pretty sure the answer is yes.

CCSpriteBatchNode doesn't have anything to do with classes, it has to do with assets. The important restriction on the use of batch nodes is that every sprite in the batch needs to reference the same texture atlas. So it is perfectly fine to have one batch node for your entire application and have every gameplay class add its own sprites to that batch. This can turn into a practical issue if your texture atlas becomes larger than the maximum texture size on your target hardware (see iOS device specs for details), but if you still want to have global batches and lots of assets it's not too hard to create a batch pool indexed by texture ID, create one batch node per atlas, and whenever you create a new sprite add it to the appropriate batch.

Honestly, I feel like the whole batch node thing is a terrible kludge on Cocos2D's part that could be made almost completely transparent to developers while still retaining its efficiency, but maybe this opinion is not entirely fair since I haven't dug around in the rendering code to understand their motivations. I guess it would mess with expectations of how depth sorting works, etc., but still I don't understand why batching objects for render is made the programmer's responsibility, it should be done by the engine.

Edit to add possible solution:

-(id) initWithMainClass:(MainClass*)mc
{

   if( (self=[super init])) {

        self.bg = [CCSprite spriteWithSpriteFrameName: @"cCircularBG.png"];
        [mc.batchNodeGeneral addChild:self.bg];

        self.dr = [CCSprite spriteWithSpriteFrameName: @"cCircularDR.png"];
        [mc.batchNodeGeneral addChild:self.dr];      
  }
  return self;  // self is a CCLayer
}
`

So when you initialize one of the other classes, you pass the main class instance as a parameter so you can access its fields. Or make the main/manager class a singleton or find another architecture suitable to your needs.

这篇关于Cocos2D 2.0 - 一个CCSpriteBatchNode可以用于多个类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆