cocos2dx 加载进度条使用
在游戏启动前,通常需要提前加载游戏中所需的资源,这样可以避免在游戏运行时因频繁加载资源导致内存飙升和卡顿问题。当加载资源到缓存时,会有一定的等待时间,为了提升用户体验,我们可以设置一个加载场景,显示进度条,待资源加载完成后,再切换到主游戏场景。
1. 进度条所在的头文件
class LoadingScene : public cocos2d::CCLayer {
public:
CREATE_FUNC(LoadingScene);
bool init();
static cocos2d::CCScene* scene();
LoadingScene();
~LoadingScene();
void resources(); // 需要加载的资源都放到这个函数里
void loadCallBack(cocos2d::CCObject* object); // 异步加载完成后回调主程序的函数
private:
int count; // 加载计数
int total; // 总资源个数
cocos2d::CCLabelTTF* label; // 显示加载进度文字
};
2. 实现部分
#include "LoadingScene.h"
#include "HelloWorldScene.h"
USING_NS_CC;
bool LoadingScene::init() {
count = 0;
total = 51;
CCSize screen = CCDirector::sharedDirector()->getVisibleSize();
float width = screen.width;
float height = screen.height;
// 进度条背景
CCSprite* loadBg = CCSprite::create("gmbg/lodingbg.png");
loadBg->setPosition(ccp(width / 2, height / 2));
this->addChild(loadBg);
// 进度条
CCSprite* loading = CCSprite::create("gmbg/longding.png");
CCProgressTimer* pt = CCProgressTimer::create(loading);
pt->setMidpoint(ccp(0, 0));
pt->setType(kCCProgressTimerTypeBar);
pt->setBarChangeRate(ccp(1, 0));
pt->setPercentage(0);
pt->setPosition(ccp(width / 2, height / 2 - 5));
this->addChild(pt, 1, 3);
label = CCLabelTTF::create("Loading ……", "arial", 30);
label->setColor(ccc3(255, 255, 0));
label->setPosition(ccp(width / 2, height / 2 + 100));
this->addChild(label, 2, 5);
this->resources();
return true;
}
CCScene* LoadingScene::scene() {
CCScene* scene = CCScene::create();
scene->addChild(LoadingScene::create());
return scene;
}
void LoadingScene::resources() {
// 异步加载各种图片资源
CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/welcomebg.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 欢迎界面 背景图片
CCTextureCache::sharedTextureCache()->addImageAsync("gmbg/coder.png", this, callfuncO_selector(LoadingScene::loadCallBack)); // 添加关于开发者背景图片
// 此处省略大量资源加载代码,可根据实际情况添加或修改
CCTextureCache::sharedTextureCache()->addImageAsync("gmme/upup.png", this, callfuncO_selector(LoadingScene::loadCallBack));
}
void LoadingScene::loadCallBack(CCObject* object) {
count++;
CCProgressTimer* pt = (CCProgressTimer*)this->getChildByTag(3);
int percentage = (int)count * 100 / total;
char tem[32];
sprintf(tem, "Loading %d%%", percentage);
CCLOG(tem);
label->setString(tem);
pt->setPercentage(percentage);
if (count == total) {
// 加载完成就切换场景
CCScene* hello = HelloWorld::scene();
CCTransitionMoveInL* effect = CCTransitionMoveInL::create(2.0f, hello);
CCDirector::sharedDirector()->replaceScene(effect);
}
}
LoadingScene::LoadingScene() {}
LoadingScene::~LoadingScene() {}
3. 切换场景后从缓存加载资源
切换场景后,我们可以直接在另一场景中从缓存中加载资源,示例代码如下:
CCTexture2D* texture = CCTextureCache::sharedTextureCache()->textureForKey("game/MagicMatrix.png");
CCSprite* sprite = CCSprite::createWithTexture(texture);
sprite->setPosition(ccp(width / 2, height / 2));
this->addChild(sprite);
4. 效果图说明
- 加载资源中:显示加载进度条,实时更新加载百分比。
- 加载完成,切换场景:当资源加载完成后,以动画效果切换到主游戏场景。
- 在另一场景中直接从缓存加载图片资源:如示例代码所示,可直接从缓存中获取已加载的图片资源,例如中间的咒符图片。
通过以上步骤,我们可以实现一个简单的 Cocos2d-x 加载进度条功能,提升游戏的用户体验。