上一篇呢,介绍的是擦美女,挺好玩的,这篇咱们说一下我们的进度条的加载,先上图
这种过渡,我们在游戏里其实是经常看到的,下面呢,我就解析一下进度条的实现
上代码:
首先,我们建一个工程,就HelloWorld吧,然后我们更改一下它的回调函数:
HelloWorld.h
public:
LabelTTF* percentLabel;
LoadingBar* pero;
int m_numSp ;
int m_loadedSp ;
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
void plistImageAsyncCallback(Texture2D* texture);
void loadingCallBack(Ref * sender);
void loadSeccess();
下面是.cpp
这里我们用到了cocos studio的只是,如果不太懂,就先跟着做,或者可以联系我
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
m_numSp = 3; //需要加载的资源总数
m_loadedSp = 0;//当前已经加载资源个数
//加载进度条文件
Widget * uiLayer = GUIReader::getInstance()->widgetFromJsonFile("jindutiao/NewUi_1.json");//导入.json文件
this->addChild(uiLayer);
//创建显示Loading: 的label ,创建一个label
auto loadLabel = LabelTTF::create("Loading:","Arial",20);
loadLabel->setPosition(Point(visibleSize.width/2-30,visibleSize.height/2+30));
this->addChild(loadLabel,1);
//创建显示百分比的label
percentLabel = LabelTTF::create("0%","Arial",20);
percentLabel->setPosition(Point(visibleSize.width/2+35,visibleSize.height/2+30));
this->addChild(percentLabel,2);
pero = (LoadingBar *) uiLayer->getChildByName("ProgressBar_1");
pero->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
//音效预加载
SimpleAudioEngine::sharedEngine()-> preloadEffect("elplo.wav");
SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("acc.mp3");
//资源的加载
auto cache = Director::getInstance()->getTextureCache();//加载
//加载.png
cache->addImageAsync("HelloWorld.png",CC_CALLBACK_1(HelloWorld::loadingCallBack,this));
//cache->addImageAsync("dabaojian2.jpg",CC_CALLBACK_1(HelloWorld::loadingCallBack,this));
//加载.plist文件
cache->addImageAsync("dabaojian.png",CC_CALLBACK_1(HelloWorld::loadingCallBack,this));
cache->addImageAsync("dabaojian.png",CC_CALLBACK_1(HelloWorld::plistImageAsyncCallback,this));
cache->addImageAsync("Desktop.png",CC_CALLBACK_1(HelloWorld::loadingCallBack,this));
return true;
}
//加载.plist文件函数
void HelloWorld::plistImageAsyncCallback(Texture2D* texture)
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("dabaojian.plist");
//SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Desktop.plist");
}
//主回调函数,实现进度过渡
void HelloWorld::loadingCallBack(Ref * sender) {
++m_loadedSp;
float newPercent = ((float)m_loadedSp / (float)m_numSp) * 100;//计算进度条当前的百分比
int n = (int)newPercent;
std::string num = StringUtils::format("%d%%",n);
percentLabel->setString(num);//更新percentLabel的值
pero->setPercent(newPercent);//更新进度条
log("end");
if (newPercent == 100)
{
loadSeccess();
}
}
//切换场景
void HelloWorld::loadSeccess() {
auto delaTime = DelayTime::create(3);
CallFunc * callFunc = CallFunc::create([=](){
auto scene = Hours::createScene();
Director::getInstance()->replaceScene(scene);});
this->runAction(Sequence::create(delaTime,callFunc,NULL));
}
这里呢,下一个场景的源码我就不往上面粘了,你可以自己建一个场景,创建你自己的场景过渡
好了,这篇博客呢,我们就到这里了,欢迎留言和指正错误,qq:3031260489