cocos2d-x 3.x学习之进度条

<pre name="code" class="cpp">异步加载
#include "cocos2d.h"
#include "Actions.hpp"
USING_NS_CC;
class HelloWorld : public cocos2d::Layer
{
public:
    HelloWorld():m_numSp(8),m_loadedSp(0),loadProgress(NULL){};
    void loadingCallback(Object *pSender);//加载一张图片完成后跳转的回调函数
    void gotoNewLayer();//加载完后跳转的函数
    static Scene* createScene();
    virtual bool init();
    void menuCloseCallback(Ref* pSender);
    void addImages();
    CREATE_FUNC(HelloWorld);
private:
    ProgressTimer *loadProgress;//进度条
    Label *percentLabel;//加载进度label
    Label *loadLabel;//显示 loading:的label
    int m_numSp;//要加载的精灵数目,初始化为8张
    int m_loadedSp;//已加载的精灵数目
};


 

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene(){
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}
bool HelloWorld::init(){
    if ( !Layer::init() ){
        return false;
    }
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    loadLabel = Label::create("Loading","Arial",20);//创建显示Loading
    loadLabel->setPosition(Vec2(visibleSize.width / 2 - 30, visibleSize.height / 2 + 30));
    this->addChild(loadLabel,1);
    percentLabel = Label::create("0%","Arial",20);//创建显示百分比的label
    percentLabel->setPosition(Vec2(visibleSize.width / 2 + 35, visibleSize.height / 2 + 30));
    this->addChild(percentLabel,2);
    loadProgress = ProgressTimer::create(Sprite::create("progressbar.png"));
    loadProgress->setScale(0.5f);
    loadProgress->setBarChangeRate(Vec2(1, 0));
    loadProgress->setType(ProgressTimer::Type::BAR);
    loadProgress->setMidpoint(Vec2(0, 1));
    loadProgress->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
    loadProgress->setPercentage(0.0f);
    this->addChild(loadProgress,2);
    addImages();
    return true;
}
void HelloWorld::addImages(){
    Director::getInstance()->getTextureCache()->addImageAsync("run1.png",CC_CALLBACK_1(HelloWorld::loadingCallback, this));
    Director::getInstance()->getTextureCache()->addImageAsync("run2.png",CC_CALLBACK_1(HelloWorld::loadingCallback, this));
    Director::getInstance()->getTextureCache()->addImageAsync("run3.png",CC_CALLBACK_1(HelloWorld::loadingCallback, this));
    Director::getInstance()->getTextureCache()->addImageAsync("run4.png",CC_CALLBACK_1(HelloWorld::loadingCallback, this));
    Director::getInstance()->getTextureCache()->addImageAsync("run5.png",CC_CALLBACK_1(HelloWorld::loadingCallback, this));
    Director::getInstance()->getTextureCache()->addImageAsync("run6.png",CC_CALLBACK_1(HelloWorld::loadingCallback, this));
    Director::getInstance()->getTextureCache()->addImageAsync("run7.png",CC_CALLBACK_1(HelloWorld::loadingCallback, this));
    Director::getInstance()->getTextureCache()->addImageAsync("run8.png",CC_CALLBACK_1(HelloWorld::loadingCallback, this));
    
}
void HelloWorld::loadingCallback(Object *pSender){
    ++m_loadedSp;//每进入到这个函数一次,自增一次
    char buf_str[16];
    sprintf(buf_str, "%d%%",(int)(((float)m_loadedSp / m_numSp) * 100),m_numSp);
    percentLabel->setString(buf_str);//更新进度条label的值
    float newPercent = 100 - ((float)m_numSp - (float)m_loadedSp) / ((float)m_numSp / 100);//计算进度条的百分比
    //因为加载图片速度很快,没有使用ProgressTo
    loadProgress->setPercentage(newPercent);//更新进度条
    //如果图片加载完成之后
    if (m_loadedSp == m_numSp) {
        this->removeChild(loadProgress);//将添加的几个对象删除掉
        this->removeChild(percentLabel);
        this->removeChild(loadLabel);
        //加载完成既要调转到gotoNewLayer,跳转场景
        this->gotoNewLayer();
    }
}
void HelloWorld::gotoNewLayer(){
    auto scene = Actions::createScene();
    Director::getInstance()->replaceScene(scene);
    
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
同步加载

#include <stdio.h>
#include "Actions.hpp"
#include "cocos2d.h"
USING_NS_CC;
class Progresstimer:public Layer{
public:
    static Scene* createScene();
    virtual bool init();
    void ImageCache(float dt);
    void scheduleCallFunc(float dt);
    CREATE_FUNC(Progresstimer);
};

#include "Progresstimer.hpp"
Scene * Progresstimer::createScene(){
    auto scene = Scene::create();
    auto layer = Progresstimer::create();
    scene->addChild(layer);
    return scene;
}
bool Progresstimer::init(){
    if (!Layer::init()) {
        return false;
    }
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    //进度条
    auto sprite = Sprite::create("progressbar.png");
    auto progress = ProgressTimer::create(sprite);
    progress->setTag(110);
    //设置进度条的类型
    progress->setType(ProgressTimer::Type::BAR);
    //设置缩放比例
    progress->setScale(0.5f);
    //设置基准点
    progress->setMidpoint(Vec2(0, 0.5));//x表示方向,0表示从左到右,1表示从右到左,y表示起始位置为图片的中点
    //表示是否改变水平或者垂直方向的比例,设置成1表示改变,0表示不改变
    progress->setBarChangeRate(Vec2(1, 0));
    progress->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
    //设置进度条的百分比
    progress->setPercentage(0.0f);
    this->addChild(progress);
    schedule(CC_CALLBACK_1(Progresstimer::ImageCache, this),0.01,"cache");
    //进度条的作用:加载图片,把工程Resource下的图片加载到缓存中,节省加载时间,加快读取图片的速度
    return true;
}
//调度器的回调函数
void Progresstimer::scheduleCallFunc(float dt){
    static int i = 0;
    //在当前Layer上获得tag为110的子节点
    ProgressTimer *p = (ProgressTimer*)this->getChildByTag(110);
    //更新进度条的百分比
    i += 8.5;
    p->setPercentage(i);
    if (p->getPercentage() == 100) {
        unschedule("callfunc1");
    }
}
void Progresstimer::ImageCache(float dt){
    static int a = 0;
    auto d = Director::getInstance();
    auto cache = d->getTextureCache();
    //addImage同步加载图片(会阻塞进程)
    a++;
    std::string s = __String::createWithFormat("%d.png",a)->getCString();
    //加载图片纹理到缓存
    if (cache->addImage(s)) {
        //让进度条百分比增加
        scheduleCallFunc(NULL);
    }
    if (a == 12) {
        unschedule("cache");
    }
    //addImageAsync同步加载图片,会阻塞进程
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值