以下代码是在cocos中使用,plist+png 还有SpriteBatchNode播放动画的代码,备份一下,以防git被墙:
bool GameMain::init() { if( !Layer::init()) return false; Size visibleSize = CCDirector::sharedDirector()->getVisibleSize(); bg = Sprite::create("battle.png"); bg->setPosition(visibleSize.width/2, visibleSize.height/2); addChild(bg); auto batchNode = SpriteBatchNode::create("huangfeihu.png",100); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("huangfeihu.plist"); Vector<SpriteFrame*> hfhFrames(24); char str[18] = {0}; for( int i = 0; i < 24; i++ ) { sprintf(str, "huangfeihu%d.png",i); auto frame = SpriteFrameCache::getInstance()->spriteFrameByName(str); if( frame != NULL ) hfhFrames.pushBack(frame); } for( int j = 0; j < 1000; j++ ) { auto hfh = Sprite::createWithSpriteFrameName("huangfeihu0.png"); auto anim = Animation::createWithSpriteFrames( hfhFrames,0.05f ); hfh->runAction(RepeatForever::create( Animate::create(anim))); hfh->setPosition(visibleSize.width*rand()/RAND_MAX, visibleSize.height*rand()/RAND_MAX); batchNode->addChild(hfh); } this->addChild(batchNode); return true; }
结果如下:
2014-6-26补:
刚才在cocos2dx官网看了下cocos2dx-3.x的版本更新内容。发现以下:
New Renderer
Features of the new renderer:
- It has been decoupled from the Scene Graph. The
draw()
method, instead of "drawing" it sends aRenderCommand
to theRenderer
, andRenderer
is responsible for drawing the queuedRenderCommand
commands.QuadCommands
(used bySprite
andParticleSystem
objects) will be automatically batched.CustomCommand
objects allow the user to use custom OpenGL code, using a API similar to v2.2GroupCommand
objects allow to have "stacks" in the Renderer with different OpenGL values.- Auto-culling for
Sprite
objects (although, technically, Auto-culling is not performed inRenderer
code, but in theSprite
code)- Global Z ordering (local Z ordering is still supported)
Renderer features
Auto-batching
Auto-batching means that the
Renderer
will package "multiple draw calls" in just one "big draw call" (AKA batch). In order to group "draw calls" certain conditions are needed:
- It only works with
QuadCommand
commands (used by Sprite and ParticleSystem objects)- The
QuadCommands
must share the same Material ID: same Texture ID, same GLProgram and same blending function- The
QuadCommands
must consecutiveIf those conditions are met, the
Renderer
will create create a batch (one draw call) with all thoseQuadCommand
objects.In case you are unfamiliar with the OpenGL best practices, batching is very important to have decent speed in your games. The less batches (draw calls) the more performance your game is going to be.
Sprite vs. SpriteBatchNode
To summarize:
- Keep putting all your sprites in a big spritesheet
- Use the same Blending Function (just use the default one)
- Use the same Shader (just use the default one)
- And don't parent your sprites to a
SpriteBatchNode
大意是在cocos2dx 3.x中尽量避免使用SpriteNodeBatch,
尽量将Sprite的素材放到一个SpriteSheet中,
因为Sprite是使用QuadBatchCommand去渲染的,
而QuadBatchCommand默认是batch(批处理的)的。
原文参考:
https://github.com/cocos2d/cocos2d-x/blob/cocos2d-x-3.0/docs/RELEASE_NOTES.md