利用texturepacker打包工具,把要用到的图打包成一张大图,在cocos2dx中通过CCSpriteBatchNode进行渲染,能提高效率。下面直接上代码:
bool HelloWorld::init()
{
//
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
//下面是添加的代码,使用CCSpriteBatchNode提高渲染效率
cache = CCSpriteFrameCache::sharedSpriteFrameCache();
cache->addSpriteFramesWithFile("ImageTest.plist","ImageTest.png");
CCTexture2D* texture = CCTextureCache::sharedTextureCache()->textureForKey("ImageTest.png");
CCSpriteBatchNode* batch = CCSpriteBatchNode::createWithTexture(texture);
addChild(batch);
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint visibleOrigin = CCDirector::sharedDirector()->getVisibleOrigin();
CCSprite* sprite = CCSprite::createWithSpriteFrameName("loading_01.png");
for (int x = visibleOrigin.x;x < visibleOrigin.x + visibleSize.width; x+= sprite->getContentSize().width+10)
{
sprite = CCSprite::createWithSpriteFrameName("loading_01.png");
sprite->setPosition(ccp(x,visibleOrigin.y + visibleSize.height));
sprite->setAnchorPoint(ccp(0,1));
batch->addChild(sprite);
}
for (int x = visibleOrigin.x;x < visibleOrigin.x + visibleSize.width; x+= 200)
{
CCSprite* sprite = CCSprite::createWithSpriteFrameName("HelloWorld.png");
sprite->setPosition(ccp(x,visibleOrigin.y - 200 + visibleSize.height));
sprite->setScale(0.5);
sprite->setAnchorPoint(ccp(0,1));
batch->addChild(sprite);
}
//注:上面两个for实现了CCSpriteBatchNode加载同一张大图下的两张不同的图片,提高了效率
//下面CCSpriteBatchNode实现动画效果。
char fileName[50]={0};
CCAnimation* animation = CCAnimation::create();
for (int i = 1; i < 13; i++)
{
sprintf(fileName,"loading_%02d.png",i);
animation->addSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(fileName));
}
animation->setDelayPerUnit(0.05f);//控制动画的快慢 ,这里如果不加,动画不动
CCSprite* hero = CCSprite::createWithSpriteFrame(cache->spriteFrameByName("loading_01.png"));
CCAnimate* animate = CCAnimate::create(animation);
hero->runAction(CCRepeatForever::create(animate));
hero->setPosition(ccp(visibleOrigin.x + visibleSize.width/2 - 100,visibleOrigin.y+20));
hero->setAnchorPoint(ccp(1,0));
//addChild(hero);
batch->addChild(hero);
//实现点击在之后改变精灵的图,大小?
hero1 = CCMenuItemImage::create();
hero1->setNormalSpriteFrame(cache->spriteFrameByName("loading_02.png"));
hero1->setPosition(ccp(visibleOrigin.x + visibleSize.width/2 + 100,visibleOrigin.y+20));
hero1->setAnchorPoint(ccp(1,0));
CCSize content = hero1->getContentSize();
char size[50] = {0};
sprintf(size,"%.0fx%.0f",content.width,content.height);
tips = CCLabelTTF::create(size,"Arial",24);
tips->setPosition(ccp(content.width/2,content.height+20));
hero1->addChild(tips);
CCPoint p = hero1->getPosition();
CCLabelTTF* ttf = CCLabelTTF::create("click me ","Arial",24);
CCMenuItemLabel* label = CCMenuItemLabel::create(ttf,this,menu_selector(HelloWorld::clickLabelCallback));
label->setPosition(ccp(p.x+content.width/2,p.y+content.height/2));
CCPoint pos = label->getPosition();
CCMenu* menu = CCMenu::create(label,hero1,NULL);
menu->setPosition(CCPointZero);
//实现一闪一闪的效果
CCActionInterval* color_action = CCTintBy::create(0.5f, 0, -255, -255);
CCActionInterval* color_back = color_action->reverse();
CCSequence* seq = CCSequence::create(color_action, color_back, NULL);
ttf->runAction(CCRepeatForever::create(seq));
//batch->addChild(menu);//这里不能这么用,否则会报错,menu没有在那一张大图里?
addChild(menu);
flag = false;//用于检查是否点击,更换图片。
return true;
}
再上图,有图有真相,其中资源来自于互联网游戏,只用于学习。
其中第一幅图上面两行和第三行左边那个都用CCSpriteBatchNode添加上去,右边那个精灵还有显示大小和“click me”的标签,一共四个精灵。第三行左边那个精灵通过CCSpriteBatchNode实现了动画效果,animation->setDelayPerUnit(0.05f)必须被调用,否则无法产生动画的效果。
下面这幅图显示了点击click me之后精灵的图改变了,说明更换之后精灵的大小会自动随着图的改变而改变。
好了,就这么多,以后有新的知识在进行补充。为了方便以后查看, 提供点击下载代码。
参考资料:http://blog.csdn.net/musicvs/article/details/8089125、http://7097095.blog.51cto.com/7087095/1230458