cocos2d-x学习笔记(14)--CCTextureCache
本文出自http://www.wenbanana.com稻草人博客,欢迎访问!
这里我直接根据一段代码来解释CCTextureCache,代码如下:
...........................
CCSprite* sprite = CCSprite::spriteWithFile("player.png");
addChild(sprite);
......................
这是我们初学者常用的载入精灵的使用方法,下面是用另一种方式:
........
CCTexture2D* cache = CCTextureCache::sharedTextureCache()->addImage("player.png");
...........
CCSprite* sprite = CCSprite::spriteWithTexture(cache);
addChild(sprite);
...........
可能有人会认为第二中方式是用效率比较高,其实是一样的,只是第二种方式比较灵活;在游戏中,如果一幅背景要载入许多精灵,如果是在切入背景是才开始载入精灵(即第一种使用方式),一旦精灵数量太庞大,那么画面就会出现卡顿的现象,玩家就会觉得游戏卡。如果我们采用第二种方式,在切入场景前,实现将图片加载到CCTextureCache中,那么在真正调用这些图片时就显得速度很快,就不会出现卡顿的现象。在这里,再说明的一点的是,所有加载图片的方法都是作为texture加载到CCTexureCache,不仅仅是CCSprite.下面我用一个实例来说明。
step1:创建cocos2d-win32项目,并命名为textureCache;
step2:在HelloWorldScene.h中添加下面两个类:
//第二种载入图片方式 class TextureCache:public CCLayer { protected: CCTexture2D* m_cache[20]; public: TextureCache(); void addSprite(CCObject* pSender); }; //第一种载入图片方式 class SpriteLayer:public CCLayer { public: SpriteLayer(); };
step3:在HelloWorldScene.cpp中添加如下函数:
TextureCache::TextureCache() { CCSize size = CCDirector::sharedDirector()->getWinSize(); CCLabelTTF* label = CCLabelTTF::labelWithString("Click", "Arial", 30); CCMenuItemLabel* item = CCMenuItemLabel::itemWithLabel(label, this, menu_selector(TextureCache::addSprite)); CCMenu* menu = CCMenu::menuWithItem(item); addChild(menu, 1); item->setPosition(ccp(200, 150)); menu->setPosition(ccp(200, 150)); //load textures m_cache[0] = CCTextureCache::sharedTextureCache()->addImage("Images/player1.png"); m_cache[1] = CCTextureCache::sharedTextureCache()->addImage("Images/player2.png"); m_cache[2] = CCTextureCache::sharedTextureCache()->addImage("Images/player3.png"); m_cache[3] = CCTextureCache::sharedTextureCache()->addImage("Images/player4.png"); m_cache[4] = CCTextureCache::sharedTextureCache()->addImage("Images/player5.png"); m_cache[5] = CCTextureCache::sharedTextureCache()->addImage("Images/player6.png"); m_cache[6] = CCTextureCache::sharedTextureCache()->addImage("Images/player7.png"); m_cache[7] = CCTextureCache::sharedTextureCache()->addImage("Images/player8.png"); m_cache[8] = CCTextureCache::sharedTextureCache()->addImage("Images/player9.png"); m_cache[9] = CCTextureCache::sharedTextureCache()->addImage("Images/player10.png"); m_cache[10] = CCTextureCache::sharedTextureCache()->addImage("Images/player11.png"); m_cache[11] = CCTextureCache::sharedTextureCache()->addImage("Images/player12.png"); m_cache[12] = CCTextureCache::sharedTextureCache()->addImage("Images/player13.png"); m_cache[13] = CCTextureCache::sharedTextureCache()->addImage("Images/player14.png"); m_cache[14] = CCTextureCache::sharedTextureCache()->addImage("Images/player15.png"); m_cache[15] = CCTextureCache::sharedTextureCache()->addImage("Images/player16.png"); m_cache[16] = CCTextureCache::sharedTextureCache()->addImage("Images/player17.png"); m_cache[17] = CCTextureCache::sharedTextureCache()->addImage("Images/player18.png"); m_cache[18] = CCTextureCache::sharedTextureCache()->addImage("Images/player19.png"); m_cache[19] = CCTextureCache::sharedTextureCache()->addImage("Images/player20.png"); } void TextureCache::addSprite(CCObject* pSender) { CCSize size = CCDirector::sharedDirector()->getWinSize(); //add sprites CCSprite* background = CCSprite::spriteWithFile("HelloWorld.png"); background->setPosition(ccp(size.width / 2, size.height / 2)); CCSprite* s1 = CCSprite::spriteWithTexture(m_cache[0]); CCSprite* s2 = CCSprite::spriteWithTexture(m_cache[1]); CCSprite* s3 = CCSprite::spriteWithTexture(m_cache[2]); CCSprite* s4 = CCSprite::spriteWithTexture(m_cache[3]); CCSprite* s5 = CCSprite::spriteWithTexture(m_cache[4]); CCSprite* s6 = CCSprite::spriteWithTexture(m_cache[5]); CCSprite* s7 = CCSprite::spriteWithTexture(m_cache[6]); CCSprite* s8 = CCSprite::spriteWithTexture(m_cache[7]); CCSprite* s9 = CCSprite::spriteWithTexture(m_cache[8]); CCSprite* s10 = CCSprite::spriteWithTexture(m_cache[9]); CCSprite* s11 = CCSprite::spriteWithTexture(m_cache[10]); CCSprite* s12 = CCSprite::spriteWithTexture(m_cache[11]); CCSprite* s13 = CCSprite::spriteWithTexture(m_cache[12]); CCSprite* s14 = CCSprite::spriteWithTexture(m_cache[13]); CCSprite* s15 = CCSprite::spriteWithTexture(m_cache[14]); CCSprite* s16 = CCSprite::spriteWithTexture(m_cache[15]); CCSprite* s17 = CCSprite::spriteWithTexture(m_cache[16]); CCSprite* s18 = CCSprite::spriteWithTexture(m_cache[17]); CCSprite* s19 = CCSprite::spriteWithTexture(m_cache[18]); CCSprite* s20 = CCSprite::spriteWithTexture(m_cache[19]); s1->setPosition(ccp(50, 50)); s2->setPosition(ccp(60, 50)); s3->setPosition(ccp(70, 50)); s4->setPosition(ccp(80, 50)); s5->setPosition(ccp(90, 50)); s6->setPosition(ccp(100, 50)); s7->setPosition(ccp(110, 50)); s8->setPosition(ccp(120, 50)); s9->setPosition(ccp(130, 50)); s10->setPosition(ccp(140, 50)); s11->setPosition(ccp(150, 50)); s12->setPosition(ccp(160, 50)); s13->setPosition(ccp(170, 50)); s14->setPosition(ccp(180, 50)); s15->setPosition(ccp(190, 50)); s16->setPosition(ccp(200, 50)); s17->setPosition(ccp(210, 50)); s18->setPosition(ccp(220, 50)); s19->setPosition(ccp(230, 50)); s20->setPosition(ccp(240, 50)); addChild(s1); addChild(s2); addChild(s3); addChild(s4); addChild(s5); addChild(s6); addChild(s7); addChild(s8); addChild(s9); addChild(s10); addChild(s11); addChild(s12); addChild(s13); addChild(s14); addChild(s15); addChild(s16); addChild(s17); addChild(s18); addChild(s19); addChild(s20); }
SpriteLayer::SpriteLayer() { CCSprite* s1 = CCSprite::spriteWithFile("Images/player1.png"); CCSprite* s2 = CCSprite::spriteWithFile("Images/player2.png"); CCSprite* s3 = CCSprite::spriteWithFile("Images/player3.png"); CCSprite* s4 = CCSprite::spriteWithFile("Images/player4.png"); CCSprite* s5 = CCSprite::spriteWithFile("Images/player5.png"); CCSprite* s6 = CCSprite::spriteWithFile("Images/player6.png"); CCSprite* s7 = CCSprite::spriteWithFile("Images/player7.png"); CCSprite* s8 = CCSprite::spriteWithFile("Images/player8.png"); CCSprite* s9 =CCSprite::spriteWithFile("Images/player9.png"); CCSprite* s10 = CCSprite::spriteWithFile("Images/player10.png"); CCSprite* s11 = CCSprite::spriteWithFile("Images/player11.png"); CCSprite* s12 = CCSprite::spriteWithFile("Images/player12.png"); CCSprite* s13 = CCSprite::spriteWithFile("Images/player13.png"); CCSprite* s14 = CCSprite::spriteWithFile("Images/player14.png"); CCSprite* s15 = CCSprite::spriteWithFile("Images/player15.png"); CCSprite* s16 = CCSprite::spriteWithFile("Images/player16.png"); CCSprite* s17 = CCSprite::spriteWithFile("Images/player17.png"); CCSprite* s18 = CCSprite::spriteWithFile("Images/player18.png"); CCSprite* s19 = CCSprite::spriteWithFile("Images/player19.png"); CCSprite* s20 = CCSprite::spriteWithFile("Images/player20.png"); s1->setPosition(ccp(50, 50)); s2->setPosition(ccp(60, 50)); s3->setPosition(ccp(70, 50)); s4->setPosition(ccp(80, 50)); s5->setPosition(ccp(90, 50)); s6->setPosition(ccp(100, 50)); s7->setPosition(ccp(110, 50)); s8->setPosition(ccp(120, 50)); s9->setPosition(ccp(130, 50)); s10->setPosition(ccp(140, 50)); s11->setPosition(ccp(150, 50)); s12->setPosition(ccp(160, 50)); s13->setPosition(ccp(170, 50)); s14->setPosition(ccp(180, 50)); s15->setPosition(ccp(190, 50)); s16->setPosition(ccp(200, 50)); s17->setPosition(ccp(210, 50)); s18->setPosition(ccp(220, 50)); s19->setPosition(ccp(230, 50)); s20->setPosition(ccp(240, 50)); addChild(s1); addChild(s2); addChild(s3); addChild(s4); addChild(s5); addChild(s6); addChild(s7); addChild(s8); addChild(s9); addChild(s10); addChild(s11); addChild(s12); addChild(s13); addChild(s14); addChild(s15); addChild(s16); addChild(s17); addChild(s18); addChild(s19); addChild(s20); }
并对HelloWorld::scene函数进行修改:
CCScene* HelloWorld::scene() { CCScene * scene = NULL; do { // 'scene' is an autorelease object scene = CCScene::node(); CC_BREAK_IF(! scene); TextureCache* layer = new TextureCache(); //SpriteLayer* layer = new SpriteLayer(); CC_BREAK_IF(! layer); // add layer as a child to scene scene->addChild(layer); } while (0); // return the scene return scene; }
step4:编译运行程序,由于这里使用的图片不是非常多,如果想要看到明显的效果,可以尝试自己添加更多的图片
源代码下载地址:http://download.csdn.net/download/wen294299195/4525827