cocos2d - CCSpriteBatchNode的使用 .

6707人阅读 评论(0) 收藏 举报

 

有点:CCSpriteBatchNode 中的所有CCSprite只会被渲染1次,因此可以提高游戏的FPS。

限制:加入到 CCSpriteBatchNode 中的CCSprite必须使用同一张纹理图。

 

问:什么时候应该用CCSpriteBatchNode?

答:比如游戏中的子弹 就很适合用它,因为子弹都是一个样子。

答:通过TexturePacker生成的纹理图也适合使用它。

 

看一个简单的Demo:

 

  1. CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithFile:@"shopAmber.png"];//初始化时给一张纹理图   
  2. [self addChild:batch];//加入到当前Layer   
  3.           
  4. CCSprite *spr = [CCSprite spriteWithFile:@"shopAmber.png"];//切记! 这里的纹理图必须和上面相同,否则会崩溃~   
  5. spr.position = ccp(10,10);  
  6. [batch addChild:spr z:2];  
  7.           
  8. CCSprite *spr2 = [CCSprite spriteWithFile:@"shopAmber.png"];  
  9. spr2.position = ccp(10,40);  
  10. [batch addChild:spr2 z:1];//可以指定z坐标。  
CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithFile:@"shopAmber.png"];//初始化时给一张纹理图
[self addChild:batch];//加入到当前Layer
		
CCSprite *spr = [CCSprite spriteWithFile:@"shopAmber.png"];//切记! 这里的纹理图必须和上面相同,否则会崩溃~
spr.position = ccp(10,10);
[batch addChild:spr z:2];
		
CCSprite *spr2 = [CCSprite spriteWithFile:@"shopAmber.png"];
spr2.position = ccp(10,40);
[batch addChild:spr2 z:1];//可以指定z坐标。

 

 

下面看看它的细节:

 

  1. //创建CCSpriteBatchNode   
  2. CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithFile:@"shopAmber.png"];  
//创建CCSpriteBatchNode
CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithFile:@"shopAmber.png"];

看看 batchNodeWithFile的实现:

 

  1. +(id)batchNodeWithFile:(NSString*) imageFile  
  2. {  
  3.     return [[[self alloc] initWithFile:imageFile capacity:defaultCapacity] autorelease];//defaultCapacity==29默认可以addChild29个精灵,应该会自动扩充<PRE class=java name="code">}   
  4. </PRE>  
  5. <PRE></PRE>  
  6. <P></P>  
  7. <PRE></PRE>  
  8. <P></P>  
  9. <P>再看看 initWithFile的实现:</P>  
  10. <P></P>  
  11. <PRE class=java name="code">-(id)initWithFile:(NSString *)fileImage capacity:(NSUInteger)capacity  
  12. {  
  13.        //看看其实就是被加载成了一张2d纹理图。   
  14.        CCTexture2D *tex = [[CCTextureCache sharedTextureCache] addImage:fileImage];  
  15.     return [self initWithTexture:tex capacity:capacity];  
  16. }</PRE>  
  17. <P></P>  
  18. <P></P>  
  19. <PRE class=java name="code">[self addChild:batch];//把CCSpriteBatchNode加入当前Layer,batch就相当于一个Layer</PRE><BR>   
  20. 之后你向CCSpriteBatchNode里加精灵 就相当于向一个层里加精灵:  
  21. <P></P>  
  22. <P></P>  
  23. <PRE class=java name="code">[batch addChild:spr z:2];</PRE><BR>  
  24. <P>可以使用 CCSpriteFrameCache配合CCSpriteBatchNode一起使用,效率会更高:</P>  
  25. <P><PRE class=java name="code">[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Resources.plist"];  
  26.           
  27. CCSprite *spr = [CCSprite spriteWithSpriteFrameName:@"Icon.png"];  
  28. spr.position = ccp(10,10);  
  29. [batch addChild:spr z:2];  
  30.           
  31. CCSprite *spr2 = [CCSprite spriteWithSpriteFrameName:@"shopAmber.png"];  
  32. spr2.position = ccp(10,40);  
  33. [batch addChild:spr2 z:1];  
  34. </PRE>这样看上去使用了2张不同的图片,但是它们是在同一张纹理图里的。<BR>  
  35. <BR>  
  36. <P></P>  
  37. <P></P>  
  38. <P></P>  
+(id)batchNodeWithFile:(NSString*) imageFile
{
	return [[[self alloc] initWithFile:imageFile capacity:defaultCapacity] autorelease];//defaultCapacity==29默认可以addChild29个精灵,应该会自动扩充
  1. }  
}

 


 

再看看 initWithFile的实现:

 

  1. -(id)initWithFile:(NSString *)fileImage capacity:(NSUInteger)capacity  
  2. {  
  3.        //看看其实就是被加载成了一张2d纹理图。   
  4.        CCTexture2D *tex = [[CCTextureCache sharedTextureCache] addImage:fileImage];  
  5.     return [self initWithTexture:tex capacity:capacity];  
  6. }  
-(id)initWithFile:(NSString *)fileImage capacity:(NSUInteger)capacity
{
       //看看其实就是被加载成了一张2d纹理图。
       CCTexture2D *tex = [[CCTextureCache sharedTextureCache] addImage:fileImage];
	return [self initWithTexture:tex capacity:capacity];
}

 

 

  1. [self addChild:batch];//把CCSpriteBatchNode加入当前Layer,batch就相当于一个Layer  
[self addChild:batch];//把CCSpriteBatchNode加入当前Layer,batch就相当于一个Layer

之后你向CCSpriteBatchNode里加精灵 就相当于向一个层里加精灵:

 

 

  1. [batch addChild:spr z:2];  
[batch addChild:spr z:2];

可以使用 CCSpriteFrameCache配合CCSpriteBatchNode一起使用,效率会更高:

  1. [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Resources.plist"];  
  2.           
  3. CCSprite *spr = [CCSprite spriteWithSpriteFrameName:@"Icon.png"];  
  4. spr.position = ccp(10,10);  
  5. [batch addChild:spr z:2];  
  6.           
  7. CCSprite *spr2 = [CCSprite spriteWithSpriteFrameName:@"shopAmber.png"];  
  8. spr2.position = ccp(10,40);  
  9. [batch addChild:spr2 z:1];  
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Resources.plist"];
		
CCSprite *spr = [CCSprite spriteWithSpriteFrameName:@"Icon.png"];
spr.position = ccp(10,10);
[batch addChild:spr z:2];
		
CCSprite *spr2 = [CCSprite spriteWithSpriteFrameName:@"shopAmber.png"];
spr2.position = ccp(10,40);
[batch addChild:spr2 z:1];

这样看上去使用了2张不同的图片,但是它们是在同一张纹理图里的。

 

posted @ 2013-05-27 14:11  六界剑仙  阅读(137)  评论(0)    收藏  举报