有時候場景中的資源加載過多的話就會引起游戲進入的時候很卡,因為那是邊加載邊顯示。在tests例子裡面有一個很好的例子叫做TextureCacheTest,裡面講解了如何寫loading。
#include "LoadingScene.h" #include "HelloWorldScene.h" bool LoadingScene::init() { totalNum=9; //記錄總的加載數量 haveLoadedNum=0; //記錄已加載的數量 this->loading(); return true; } CCScene *LoadingScene::scene() { CCScene *scene=CCScene::create(); LoadingScene *layer=LoadingScene::create(); scene->addChild(layer); return scene; } void LoadingScene::loading() { CCSize size=CCDirector::sharedDirector()->getWinSize(); ttf=CCLabelTTF::create("%0", "Arial", 12); //顯示加載進度 CCLabelTTF *havettf=CCLabelTTF::create("Loading", "Arial", 12); this->addChild(ttf,1); this->addChild(havettf,1); ttf->setPosition(ccp(size.width/3, size.height/2)); havettf->setPosition(ccp(size.width/2, size.height/2)); CCTextureCache::sharedTextureCache()->addImageAsync("youlost.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("youwin.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("cat.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("catBody1.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("catBody2-4.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("catBody3.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("catHand1.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("catHand2.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("catTail.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); } void LoadingScene::loadedCallBack() { haveLoadedNum++; this->runAction(CCDelayTime::create(15)); char tmp[10]; sprintf(tmp, "%%%d",(int)((float)haveLoadedNum/totalNum*100)); ttf->setString(tmp); //更改加載進度 if (haveLoadedNum==9) { this->removeChild(ttf, true); //加載完成後,移除加載進度顯示 CCScene *newscne=HelloWorld::scene(); CCDirector::sharedDirector()->replaceScene(newscne); //場景切換 } } #include "LoadingScene.h" #include "HelloWorldScene.h" bool LoadingScene::init() { totalNum=9; //記錄總的加載數量 haveLoadedNum=0; //記錄已加載的數量 this->loading(); return true; } CCScene *LoadingScene::scene() { CCScene *scene=CCScene::create(); LoadingScene *layer=LoadingScene::create(); scene->addChild(layer); return scene; } void LoadingScene::loading() { CCSize size=CCDirector::sharedDirector()->getWinSize(); ttf=CCLabelTTF::create("%0", "Arial", 12); //顯示加載進度 CCLabelTTF *havettf=CCLabelTTF::create("Loading", "Arial", 12); this->addChild(ttf,1); this->addChild(havettf,1); ttf->setPosition(ccp(size.width/3, size.height/2)); havettf->setPosition(ccp(size.width/2, size.height/2)); CCTextureCache::sharedTextureCache()->addImageAsync("youlost.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("youwin.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("cat.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("catBody1.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("catBody2-4.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("catBody3.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("catHand1.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("catHand2.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); CCTextureCache::sharedTextureCache()->addImageAsync("catTail.png", this, callfuncO_selector(LoadingScene::loadedCallBack)); } void LoadingScene::loadedCallBack() { haveLoadedNum++; this->runAction(CCDelayTime::create(15)); char tmp[10]; sprintf(tmp, "%%%d",(int)((float)haveLoadedNum/totalNum*100)); ttf->setString(tmp); //更改加載進度 if (haveLoadedNum==9) { this->removeChild(ttf, true); //加載完成後,移除加載進度顯示 CCScene *newscne=HelloWorld::scene(); CCDirector::sharedDirector()->replaceScene(newscne); //場景切換 } }
這樣,在HelloWorld中,就可以通過
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSprite *sp=CCSprite::createWithTexture(CCTextureCache::sharedTextureCache()->textureForKey("youlost.png")); addChild(sp,1); } bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSprite *sp=CCSprite::createWithTexture(CCTextureCache::sharedTextureCache()->textureForKey("youlost.png")); addChild(sp,1);
}來獲得預加載的圖片,從而緩解游戲初步加載時的卡現象。