cocos2d-js进度条

当我们进入一个游戏的时候首先要考虑到他的加载界面问题。

首先我们进入cc.LoaderScene中查看代码

如果用默认的方式的话并不是一个进度条、而是一个Label

源码:

 //loading percent
var label = self._label = new cc.LabelTTF("Loading... 0%", "Arial", fontSize);
label.setPosition(cc.pAdd(cc.visibleRect.center, cc.p(0, lblHeight)));
label.setColor(cc.color(180, 180, 180));
bgLayer.addChild(this._label, 10);
return true;

更新百分比的地方是:


<pre name="code" class="java">_startLoading: function () {
        var self = this;
        self.unschedule(self._startLoading);
        var res = self.resources;
        cc.loader.load(res,
            function (result, count, loadedCount) {
                var percent = (loadedCount / count * 100) | 0;
                percent = Math.min(percent, 100);
                self._label.setString("Loading... " + percent + "%");
            }, function () {
                if (self.cb)
                    self.cb.call(self.target);
            });
    }
 所以当我们要把Label改成一个进度条的 时候就应该把创建Label改成创建进度条 

创建进度条有两种情况: 一种是Layer

代码:

this._processLayer = cc.LayerColor.create(cc.color(255, 100, 100, 128), 1, 30);
this._processLayer.setPosition(cc.pAdd(centerPos, cc.p(- this._processLayerLength / 2, -logoHeight / 2 - 50)));
// this._processLayer.ignoreAnchorPointForPosition(false);
// this._processLayer.setAnchorPoint(cc.p(0, 0));
this._bgLayer.addChild(this._processLayer);
然后在_startLoading中修改更新方法:
	
_startLoading: function () {
        var self = this;
        self.unschedule(self._startLoading);
        var res = self.resources;
        cc.loader.load(res,
            function (result, count, loadedCount) {
                var percent = (loadedCount / count * 100) | 0;
                percent = Math.min(percent, 100);
               // self._label.setString("Loading... " + percent + "%");
               //修改成
                this._processLayer && this._processLayer.changeWidth(percent);
            }, function () {
                if (self.cb)
                    self.cb.call(self.target);
            });
    }
}

另外一种是用ccui.LoadingBar()创建一个进度条来实现:
var loadingBar  = new ccui.LoadingBar();
loadingBar.setName("LoadingBar");
loadingBar.loadTexture(res.loadingbar);
loadingBar.setPercent(0);
loadingBar.setPosition(cc.pAdd(cc.visibleRect.center, cc.p(0, lblHeight)));
bgLayer.addChild(loadingBar, 10);
this._loadingbar = loadingBar;

在_startLoading中修改代码:
	
this._loadingBar && this._loadingBar.setPercent(this._count);







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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值