设为首页 加入收藏

TOP

cocos2d-x Loading界面实现资源加载 (一)
2014-11-23 21:27:53 来源: 作者: 【 】 浏览:10
Tags:cocos2d-x Loading 界面 实现 资源 加载

有时候场景中的资源加载过多的话就会引起游戏进入的时候很卡,因为那是边加载边显示。在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::sharedTextureCa
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDU 4705 Y 下一篇hdu 1879 继续畅通工程 最小生成..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·如何理解c语言指针和 (2025-12-27 01:19:11)
·为什么C标准库没有链 (2025-12-27 01:19:08)
·玩转C语言和数据结构 (2025-12-27 01:19:05)
·MySQL 基础入门视频 (2025-12-26 23:20:22)
·小白入门:MySQL超详 (2025-12-26 23:20:19)