/** * 资源加载控制器 */ cc.Class({ extends: cc.Component, properties: { loadPrecentLabel: cc.Label }, onLoad () { this.percent = 0; }, backKeyOnClicked () { return this.node.active; }, showLoadingAlert () { this.percent = 0; this.loadPrecentLabel.string = '已加载' + this.percent + '%'; this.node.active = true; // 避免每个场景更新一遍ctrl // 这里直接通过getChildByName获取子节点 let bg = this.node.getChildByName('bg'); let icon = this.node.getChildByName('icon'); let precent = this.node.getChildByName('precent'); if (bg) { bg.active = false; } if (icon) { icon.active = false; } if (precent) { precent.active = false; } this.scheduleOnce(() => { if (bg) { bg.active = true; } if (icon) { icon.active = true; } if (precent) { precent.active = true; } }, 1); }, updateLoadingProgress (completedCount, totalCount, item) { if (completedCount == totalCount) { this.node.active = false; this.percent = 0; return; } // 显示加载进度 // totalCount 会在加载过程中增加导致总进度变小 // 所以需要比较之前缓存进度大小,避免进度后退的情况 let percent = Math.floor(completedCount / totalCount * 100); if (percent > this.percent) { this.percent = percent; } this.loadPrecentLabel.string = '已加载' + this.percent + '%'; }, /** * 显示提示 * * @author libo * @date 2019-07-04 * @param {*} tips */ showTips (tips, duration, cb) { // 显示tips this.node.active = true; this.loadPrecentLabel.string = tips; if (G.FuncUtils.isNumber(duration) && duration > 0) { cc.tween(this.node) .delay(duration) .call(() => { this.hideTips(); if (cb) { cb(); } }) .start(); } }, /** * 隐藏 * * @author libo * @date 2019-07-04 */ hideTips () { // 隐藏tips this.node.active = false; this.node.stopAllActions(); } });