cocos2d-x学习笔记(14)--CCTextureCache
这里我直接根据一段代码来解释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中添加下面两个类:
[cpp]
//第二种载入图片方式
class TextureCache:public CCLayer
{
protected:
CCTexture2D* m_cache[20];
public:
TextureCache();
void addSprite(CCObject* pSender);
};
//第一种载入图片方式
class SpriteLayer:public CCLayer
{
public:
SpriteLayer();
};
step3:在HelloWorldScene.cpp中添加如下函数:
[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));&n
补充:移动开发 , 其他 ,