您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x之 CCSpriteBatchNode 用法总结

2013-03-27 23:04 405 查看
  

例子1:

    CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithFile:@"table.png"];

        [self addChild:batch];

创建一个CCSpriteBatchNode对象,通过传递一个包含所有sprite的batch的名字作为参数,并把它加入到当前场景之中。

接下来,你从batch中创建的任何sprite,你应该把它当作CCSpriteBatchNode的一个孩子加进去。只要sprite包含在batch中,那么就没问题,否则会出错。后面加的CCSprite的名字,要不跟
batchNode创建的时候是同一个图片名字,要不就是 CCSpriteFrameCache里面的大图的小图的名字,否则会有问题。其实就是同一个纹理贴图 CCTexture2D  ,比如下面的tabletop.png必须是 table.png的小图。

        CCSprite* tableTop = [CCSprite spriteWithSpriteFrameName:@"tabletop.png"];

        tableTop.position = [Helper screenCenter];

        [batch addChild:tableTop];

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

 例子2:

   CCSpriteBatchNode* batchNode = [CCSpriteBatchNode batchNodeWithFile:@"bullet.png"];  

   [self addChild:batchNode];  

   for (int i = 0; i < 500; ++i)  

   {  
    CCSprite* bullet = [CCSprite spriteWithFile:@"bullet.png"];  

    [batchNode addChild:bullet];  
   }  

1.  创建  

      CCSpriteBatchNode* create(const
char* fileImage, unsigned
int capacity);

     CCSpriteBatchNode* create(const
char* fileImage);    

   第一个是 传入 文件名,和容量。

   第二个是直接传入文件名,容量是默认的kDefaultSpriteBatchCapacity
值为 29

   也可以通过 
CCSpriteBatchNode* createWithTexture(CCTexture2D* tex,
unsigned int capacity);

  

CCSpriteBatchNode* createWithTexture(CCTexture2D* tex);这两个方法传入  CCTexture2D对象来创建。

 2.  加入子节点

     
virtual void addChild(CCNode * child);

    virtual void addChild(CCNode * child,
int zOrder);

    virtual void addChild(CCNode * child,
int zOrder, int tag);

3.   重新改变zorder

    virtual
void reorderChild(CCNode * child,
int zOrder);

4.  移除

    virtual void removeChild(CCNode* child,
bool cleanup);

    virtual void removeAllChildrenWithCleanup(bool cleanup);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: