程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> cocos2d-x Loading界面實現資源加載

cocos2d-x Loading界面實現資源加載

編輯:C++入門知識

有時候場景中的資源加載過多的話就會引起游戲進入的時候很卡,因為那是邊加載邊顯示。在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);


 }來獲得預加載的圖片,從而緩解游戲初步加載時的卡現象。

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved