cocos2dx3.0 Loading场景

Cocos2d-x 专栏收录该内容
30 篇文章 2 订阅

进度条

超级玛丽这种简单的游戏根本不需要预加载太多东西,或者说根本不需要loading这么笨重的交互。没办法,熟悉一些方法,学习而已,别太较真。
场景的进度条使用ProgressTimer,如果我们使用cocosStudio创建的场景,就使用工具的那个。
设置一个进度条成员变量,然后:
loadProgress = ProgressTimer::create(Sprite::create("image/loadingbar.png"));  
loadProgress->setBarChangeRate(Point(1, 0));//设置进程条的变化速率  
loadProgress->setType(ProgressTimer::Type::BAR);//设置进程条的类型  
loadProgress->setMidpoint(Point(0, 1));//设置进度的运动方向  
  
loadProgress->setPosition(visibleSize.width / 2, visibleSize.height / 2 );  
loadProgress->setPercentage(progressPercent);  
this->addChild(loadProgress);  

要调整进度条,只需要在异步加载资源的回调函数中实现就可以了。
像loading场景也可以实现loading的动画等其他效果,异步加载并不会对界面造成太多的卡顿效果,除非手机实在是太烂了。。

异步加载资源
一般缓存有三种,TextureCache、SpriteFrameCache、AnimationCache。
我们loading 的时候要根据需求预加载不同的资源。 然后在回调函数中处理进度条的进度。
超级玛丽登陆loading我使用最简单的TextureCache来操作。
在init里面添加preloadResource函数,这个函数会直接执行完,不需等待任何结果。 然后资源会在后台加载。
void LoadingScene::preloadResource()  
{  
    //这里分开写测试, 后期如果确定是一个场景中的直接使用plist加载  
    std::string resouceMain = "image/mainscene/";  
    float count = 20;//一共加载十七张  
    everyAdd = 100 / count;  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"about_normal.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"about_select.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"backA.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"backB.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"background.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"bg.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"music_off.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"music_on.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"quitgame_normal.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"quitgame_select.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"Set_Music.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"Setting_n.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"setting_s.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"startgame_normal.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"startgame_select.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"wxb.jpg", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain+"zhy.jpg", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain + "sound_effect_off.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain + "sound_effect_on.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
    Director::getInstance()->getTextureCache()->addImageAsync(resouceMain + "switchBg.png", CC_CALLBACK_1(LoadingScene::loadingCallback, this));  
  
}  

回调函数中这样处理:
void LoadingScene::loadingCallback(Texture2D*)  
{  
    progessAdd();  
}  
  
void LoadingScene::progessAdd()  
{  
    progressPercent += everyAdd;  
  
    if (100-progressPercent <everyAdd)  
    {  
        progressPercent = 100;  
        auto scene = MainScene::createScene();  
        Director::getInstance()->replaceScene(TransitionFade::create(1, scene));  
    }  
    percentLabel->setString(StringUtils::format("%d", int(progressPercent)));//更新percentLabel的值  
    loadProgress->setPercentage(progressPercent);  
}  

在loading到100%的时候切换场景。
资源不是很多进度条没有+1的前进,这里也没有任何技巧性的实现方法。 还望大家有好优化的时候告诉我,一起进步,万分感谢~

上面的loading逻辑其实是错误的!
正确的应该是每异步加载完一个资源才可以加载后一个,同时最好一次加载一个,不然有可能造成内存陡升,导致应用直接被干掉!
如下写:
头文件如下:
private:  
    void update(float f);  
    std::vector<std::string> reloadResources;  
    int reloadNums = 0;  
    int curReloadNum = 0;  
    bool loading = true;  
  
    void imageAsyncCallback( cocos2d::Texture2D*);  
 

cpp如下:
bool Welcome::init()  
{  
    //  
    // 1. super init first  
    if ( !Layer::init() )  
    {  
        return false;  
    }  
      
    Size visibleSize = Director::getInstance()->getVisibleSize();  
  
    /  
    // 3. add your codes below...  
    auto sprite = Sprite::create("welcome.png");  
  
    // position the sprite on the center of the screen  
    sprite->setPosition(Point(visibleSize.width/2, visibleSize.height/2));  
  
    // add the sprite as a child to this layer  
    this->addChild(sprite, 0);  
      
    reloadResources = {"Themes/scene/mainscene.png","Themes/scene/mainscene_title.png"};  
    reloadNums = reloadResources.size();  
    this->scheduleUpdate();  
  
    return true;  
}  
  
void Welcome::update(float f)  
{  
    if (loading)  
    {  
        loading = false;  
        if (curReloadNum >= reloadNums)  
        {  
            this->unscheduleUpdate();  
            auto scene = MainScene::createScene();  
            Director::getInstance()->replaceScene(TransitionCrossFade::create(0.6, scene));  
            return;  
        }  
        else  
        {  
            Director::getInstance()->getTextureCache()->addImageAsync(reloadResources[curReloadNum],  
                CC_CALLBACK_1(Welcome::imageAsyncCallback, this));  
        }  
    }  
      
}  
  
void Welcome::imageAsyncCallback(Texture2D*)  
{  
    curReloadNum++;  
    loading = true;  
}  
这样就做到了每次只加载一个资源,每一帧来操作一次,可以实时的进行垃圾回收。
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值