CCSpriteBatchNode应用(转)

 原地址:http://www.cnblogs.com/BeemanComing/archive/2011/10/29/2228466.html

     一般游戏图片资源会打包成一张大图,这样节省空间,又提升速度。打包工具有Zwoptex和texturepacker等等。

   CCSpriteBatchNode的初始化只要一张图片,也就是那张大图。然后把所有用到那张大图里面的小图的sprite都加到 CCSpriteBatchNode的child,绘制效率就会提高。

1) 缓冲sprite帧和纹理

        // 从纹理贴图集中预加载精灵帧,这个方法做了以下几件事:

  • 寻找工程目录下面和输入的参数名字一样,但是后缀是.png的图片文件。然后把这个文件加入到共享的CCTextureCache中。
  • 解析plist文件,追踪所有的sprite在spritesheet中的位置,内部使用CCSpriteFrame对象来追踪这些信息。

[CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGBA8888];

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

CCSpriteBatchNode *spriteNode = [CCSpriteBatchNode batchNodeWithFile:@"table .png"];[self addChild:spriteNode];

 

2) 创建一个精灵批处理结点

        // 为所有 静态刚体 设置的批处理精灵节点
        CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithFile:@"table.png"];
        [self addChild:batch];

  • 创建一个CCSpriteBatchNode对象,通过传递一个包含所有sprite的batch的名字作为参数,并把它加入到当前场景之中。
  • 接下来,你从batch中创建的任何sprite,你应该把它当作CCSpriteBatchNode的一个孩子加进去。只要sprite包含在batch中,那么就没问题,否则会出错。
  •         CCSprite* tableTop = [CCSprite spriteWithSpriteFrameName:@"tabletop.png"];
            tableTop.position = [Helper screenCenter];
            [batch addChild:tableTop];

  • CCSpriteBatchNode可以智能地遍历它的所有的孩子结点,并通过一次OpenGL ES call来渲染这些孩子,而不是以前每个sprite都需要一个OpenGL call,这样渲染速度就会更快。

   -(CCSpriteBatchNode*) getSpriteBatch
   {
       return (CCSpriteBatchNode*)[self getChildByTag:kTagBatchNode];
   }

      CCSpriteBatchNode* batch = [[PinballTable sharedTable] getSpriteBatch];
      sprite = [CCSprite spriteWithSpriteFrameName:spriteFrameName];
      [batch addChild:sprite];

//Batch是一个CCSpriteBatchNode,下面Batch-》Sprite-》ChildrenSprite,

//它们的Tag分别为:TagofBatch,TagofSprite,TagofChildrenSprite 得到ChildSprite的方法如下,

根据各自的Tag得到相应的数据。

id Batch = [self getChildByTag:TagofBatch];

id Sprite = [Batch getChildByTag:TagofSprite];

CCSprite *ChildrenSprite = (CCSprite*) [Sprite getChildByTag:TagofChildrenSprite];

 

 

 

 

關於CCSpriteBatchNode,一个CCSpriteBatchNode是一种效率比较高的渲染精灵的方式。比如,你把CCSprite加到CCLayer中,那么sprite的draw函数在 每一帧调用时都会执行7个opengl 调用来完成sprite的渲染。一个精灵的时候当然没问题,但是,当你在屏幕上有200个精灵的时候,那么就会有200×7次opengl调用。。。而 CCSpriteBatchNode可以“批处理”它的孩子精灵的draw调用。这意味着,当把200个精灵加到 Spritesheet中去的时候,只要使用7个opengl调用就可以完成200个孩子的渲染了。

cocos2d裏面的描述

Detailed Description

CCSpriteBatchNode is like a batch node: if it contains children, it will draw them in 1 single OpenGL call (often known as "batch draw").

CCSpriteBatchNode can reference one and only one texture (one image file, one texture atlas). Only the CCSprites that are contained in that texture can be added to the CCSpriteBatchNode. All CCSprites added to a CCSpriteBatchNode are drawn in one OpenGL ES draw call. If the CCSprites are not added to a CCSpriteBatchNode then an OpenGL ES draw call will be needed for each one, which is less efficient.

Limitations:

  • The only object that is accepted as child (or grandchild, grand-grandchild, etc...) is CCSprite or any subclass of CCSprite. eg: particles, labels and layer can't be added to a CCSpriteBatchNode.
  • Either all its children are Aliased or Antialiased. It can't be a mix. This is because "alias" is a property of the texture, and all the sprites share the same texture.

猜你喜欢

转载自lizi07.iteye.com/blog/1609912