cocoscreator 加载场景进度

Cocos creator 2.0版本之前

cc.loader.onProgress = function (completedCount, totalCount, item){
      //先开监听
    this.loading.progress = completedCount/totalCount;
    this.loadLabel.string = Math.floor(completedCount/totalCount * 100) + "%";
}.bind(this); 

cc.director.preloadScene("sceneName", function () {
    cc.log("加载成功");
});

Cocos creator升级到2.0版本后

cc.director.preloadScene("sceneName", this.onProgress.bind(this), function(){    
    cc.log("加载成功");
})

onProgress :function(completedCount, totalCount, item){
    this.loading.progress = completedCount/totalCount;
    this.loadLabel.string = Math.floor(completedCount/totalCount * 100) + "%";
}

或者 比较 hack 的办法

var info = cc.director._getSceneUuid(this.sceneName);
var self = this;
if (info) {
    cc.loader.load({ uuid: info.uuid, type: 'uuid' }, function(completedCount, totalCount, item){
      cc.log("已完成Items:" + completedCount);
      cc.log("全部Items:" + totalCount);
      cc.log("当前Item:" + item.url);
      self._loadingNextStep = parseInt(completedCount / totalCount * 100);
      cc.log("加载进度:" + self._loadingNextStep);
    }, function(error, asset){
                if (error) {
                    cc.errorID(1210, this.sceneName, error.message);
                } else {
                    cc.log("加载完成");         }
     }
 });
}

t通过preloadScene 可以观察到

preloadScene: function (sceneName, onProgress, onLoaded) {
        if (onLoaded === undefined) {
            onLoaded = onProgress;
            onProgress = null;
        }

        var info = this._getSceneUuid(sceneName);
        if (info) {
            this.emit(cc.Director.EVENT_BEFORE_SCENE_LOADING, sceneName);
            cc.loader.load({ uuid: info.uuid, type: 'uuid' }, 
                onProgress,    
                function (error, asset) {
                    if (error) {
                        cc.errorID(1210, sceneName, error.message);
                    }
                    if (onLoaded) {
                        onLoaded(error, asset);
                    }
                });       
        }
        else {
            var error = 'Can not preload the scene "' + sceneName + '" because it is not in the build settings.';
            onLoaded(new Error(error));
            cc.error('preloadScene: ' + error);
        }
    },

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值