cocos2d - CCSpriteBatchNode的使用
来源:互联网 发布:看刀路软件 编辑:程序博客网 时间:2024/05/01 12:22
有点:CCSpriteBatchNode 中的所有CCSprite只会被渲染1次,因此可以提高游戏的FPS。
限制:加入到 CCSpriteBatchNode 中的CCSprite必须使用同一张纹理图。
问:什么时候应该用CCSpriteBatchNode?
答:比如游戏中的子弹 就很适合用它,因为子弹都是一个样子。
答:通过TexturePacker生成的纹理图也适合使用它。
看一个简单的Demo:
CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithFile:@"shopAmber.png"];//初始化时给一张纹理图[self addChild:batch];//加入到当前LayerCCSprite *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坐标。
下面看看它的细节:
//创建CCSpriteBatchNodeCCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithFile:@"shopAmber.png"];
看看 batchNodeWithFile的实现:
+(id)batchNodeWithFile:(NSString*) imageFile{return [[[self alloc] initWithFile:imageFile capacity:defaultCapacity] autorelease];//defaultCapacity==29默认可以addChild29个精灵,应该会自动扩充}再看看 initWithFile的实现:
-(id)initWithFile:(NSString *)fileImage capacity:(NSUInteger)capacity{ //看看其实就是被加载成了一张2d纹理图。 CCTexture2D *tex = [[CCTextureCache sharedTextureCache] addImage:fileImage];return [self initWithTexture:tex capacity:capacity];}[self addChild:batch];//把CCSpriteBatchNode加入当前Layer,batch就相当于一个Layer
之后你向CCSpriteBatchNode里加精灵 就相当于向一个层里加精灵:[batch addChild:spr z:2];可以使用 CCSpriteFrameCache配合CCSpriteBatchNode一起使用,效率会更高:
[[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张不同的图片,但是它们是在同一张纹理图里的。
- cocos2d - CCSpriteBatchNode的使用
- cocos2d - CCSpriteBatchNode的使用
- cocos2d - CCSpriteBatchNode的使用
- cocos2d -x 精灵集合CCSpriteBatchNode的使用
- cocos2d CCSpriteBatchNode的用法
- cocos2d中的图片使用(CCSpriteBatchNode)
- CCSpriteBatchNode 的使用
- CCSpriteBatchNode的使用
- CCSpriteBatchNode的作用,使用
- cocos2d-x中CCSpriteBatchNode使用注意
- cocos2d-x 关于CCSpriteBatchNode的深入详解
- 【爱上cocos2d-x之八】使用CCSpriteBatchNode优化绘制一批相同的精灵
- cocos2d CCSpriteBatchNode 与 CCSprite.
- Cocos2d-x-CCSpriteBatchNode
- [cocos2d-x]用CCSpriteBatchNode进行文理贴图的优化
- CCSpriteBatchNode使用限制
- iphone游戏开发之cocos2d( 十一 )使用CCSpriteBatchNode和纹理图册,提高精灵动画效率;
- Cocos2d中CCSpriteBatchNode与纹理贴图集(Texture Atlas)的作用
- Sharepoint ListTemplateId
- C# API 调用格式和参数类型
- nasm下的int 3
- jquery模糊匹配
- 关于浏览器内核的一些小知识,明明白白选浏览器
- cocos2d - CCSpriteBatchNode的使用
- Dbus简介
- 第五章习题
- fstream默认不支持中文路径和输出整数带逗号的解决办法
- 13.1.2 异步下载网页
- 关于typedef的理解
- 读书笔记10:外观模式
- 测试函数指针传参
- 如何使用XCode 4创建ipa文件及提交应用程序