CCSpriteBatchNode

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

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

1) 缓冲sprite帧和纹理

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

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


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

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];

你可能感兴趣的:(Sprite)