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);
}
},