batchnode的作用及用法

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

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

1) 缓冲sprite帧和纹理

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

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

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

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

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

  • 创建一个CCSpriteBatchNode对象,通过传递一个包含所有sprite的batch的名字作为参数,并把它加入到当前场景之中。
  • 接下来,你从batch中创建的任何sprite,你应该把它当作CCSpriteBatchNode的一个孩子加进去。只要sprite包含在batch中,那么就没问题,否则会出错。

  1. CCSprite* tableTop = [CCSprite spriteWithSpriteFrameName:@"tabletop.png"];  
  2.       tableTop.position = [Helper screenCenter];  
  3.       [batch addChild:tableTop];  
  • CCSpriteBatchNode可以智能地遍历它的所有的孩子结点,并通过一次OpenGL ES call来渲染这些孩子,而不是以前每个sprite都需要一个OpenGL call,这样渲染速度就会更快。

  1. -(CCSpriteBatchNode*) getSpriteBatch  
  2.  {  
  3.      return (CCSpriteBatchNode*)[self getChildByTag:kTagBatchNode];  
  4.  }  
  5.     CCSpriteBatchNode* batch = [[PinballTable sharedTable] getSpriteBatch];  
  6.     sprite = [CCSprite spriteWithSpriteFrameName:spriteFrameName];  
  7.     [batch addChild:sprite];  

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

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

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

  1. id Batch = [self getChildByTag:TagofBatch];  
  2. id Sprite = [Batch getChildByTag:TagofSprite];  
  3. CCSprite *ChildrenSprite = (CCSprite*) [Sprite getChildByTag:TagofChildrenSprite]; 
iphone版,转自:http://blog.csdn.net/trandy/article/details/6661226

转载于:https://www.cnblogs.com/FlyingPacer/archive/2011/08/24/batchnode.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值