cocos2d-x Loading界面实现资源加载 (二)

2014-11-23 21:27:53 · 作者: · 浏览: 15
che()->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);


}来获得预加载的图片,从而缓解游戏初步加载时的卡现象。