展会信息港展会大全

cocos2d-x Loading界面实现资源加载
来源:互联网   发布日期:2015-09-27 15:00:34   浏览:3865次  

导读: 有时候场景中的资源加载过多的话就会引起游戏进入的时候很卡,因为那是边加载边显示。在tests例子里面有一个很好的例子叫做TextureCacheTest,里面讲解了如何写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::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);

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

赞助本站

人工智能实验室
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港