使用普通方法实现批处理精灵
在Sprite.h中加入以下的代码
#ifndef __Sprite_SCENE_H__ #define __Sprite_SCENE_H__ #include "cocos2d.h" USING_NS_CC; class Sprite : public CCLayer { public: //初始化层 bool init(); //创建场景 static CCScene* scene(); //响应触摸 bool Sprite::ccTouchBegan(CCTouch* touch, CCEvent*); //用于创建:场景、菜单、层等东西 CREATE_FUNC(Sprite); }; #endif // __Sprite_SCENE_H__
在Sprite.cpp中加入以下的代码
#include "Sprite.h" USING_NS_CC; //创建场景 CCScene* Sprite::scene() { //创建场景 CCScene *scene = CCScene::create(); //创建层 Sprite *layer = Sprite::create(); //将层加到场景上 scene->addChild(layer); //返回场景 return scene; } //初始化实例句柄 bool Sprite::init() { //初始化父类CCLayer CCLayer::init(); //得到窗体的尺寸 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //设置坐标 CCPoint ptCenter = ccp(winSize.width / 2, winSize.height / 2); //创建一个批处理结点容器 CCSpriteBatchNode* batch = CCSpriteBatchNode::create("CloseNormal.png"); //将精灵加到层中 addChild(batch); //使用纹理创建精灵 CCSprite* sprite = CCSprite::createWithTexture(batch->getTexture()); //将精灵加到容器上 batch->addChild(sprite); //设置精灵的位置 sprite->setPosition(ptCenter); //设置触摸 setTouchEnabled(true); setTouchMode(kCCTouchesOneByOne); return true; } //响应触摸 bool Sprite::ccTouchBegan(CCTouch* touch, CCEvent*) { //得到窗体的尺寸 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //循环100次,创建1000个精灵 for (int i = 0; i < 1000; i++) { //创建精灵 CCSprite* sprite = CCSprite::create("CloseNormal.png"); //将精灵加入到层上 addChild(sprite); //设置精灵的位置 sprite->setPosition(ccp(CCRANDOM_0_1()*winSize.width, CCRANDOM_0_1()* winSize.height)); } return true; }
运行结果:
1、打开程序后的效果:启动程序后帧率为58.7
2、单击3次鼠标后的效果:单击三次鼠标后实现了批处理精灵帧率降低
使用纹理实现批处理精灵
在Sprite.h中加入以下的代码
#ifndef __Sprite_SCENE_H__ #define __Sprite_SCENE_H__ #include "cocos2d.h" USING_NS_CC; class Sprite : public CCLayer { public: //初始化层 bool init(); //创建场景 static CCScene* scene(); //响应触摸 bool Sprite::ccTouchBegan(CCTouch* touch, CCEvent*); CCSpriteBatchNode* _batch; //用于创建:场景、菜单、层等东西 CREATE_FUNC(Sprite); }; #endif // __Sprite_SCENE_H__
Sprite.cpp中的代码
#include "Sprite.h" USING_NS_CC; //创建场景 CCScene* Sprite::scene() { //创建场景 CCScene *scene = CCScene::create(); //创建层 Sprite *layer = Sprite::create(); //将层加到场景上 scene->addChild(layer); //返回场景 return scene; } //初始化实例句柄 bool Sprite::init() { //初始化父类CCLayer CCLayer::init(); //得到窗体的尺寸 CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //设置坐标 CCPoint ptCenter = ccp(winSize.width / 2, winSize.height / 2); //批处理精灵 CCSpriteBatchNode* batch = CCSpriteBatchNode::create("CloseNormal.png"); addChild(batch); _batch = batch; CCSprite* sprite = CCSprite::createWithTexture(batch->getTexture()); batch->addChild(sprite); sprite->setPosition(ptCenter); setTouchEnabled(true); setTouchMode(kCCTouchesOneByOne); return true; } //响应触摸 bool Sprite::ccTouchBegan(CCTouch* touch, CCEvent*) { CCSize winSize = CCDirector::sharedDirector()->getWinSize(); for (int i = 0; i < 1000; i++) { CCSprite* sprite = CCSprite::createWithTexture(_batch->getTexture()); _batch->addChild(sprite); sprite->setPosition(ccp(CCRANDOM_0_1()*winSize.width, CCRANDOM_0_1()* winSize.height)); } return true; }
运行结果:
1、打开程序后的效果:启动程序后帧率为58.7
2、单击多次后的效果,帧率没有明显变化
说明:CCSpriteBatchNode也是一个容器,可是它仅仅能包容CCSprite对象,并且要求这些精灵来自同一个纹理