cocos2d-js 数字自动滚动效果

实现方式一:
从数字1滚动到数字2,并显示增加减少数量

rollFightNum: function(num1, num2) {
    if (!this._powerLabelClone) {
        this._powerLabelClone = this._powerLabel.clone();
        this._powerBg.addChild(this._powerLabelClone);
    }
    
    if (this._powerLabelClone.isVisible()) {
        this.unschedule(this.rollFightNumAction);
    }
    
    this.roll_num1 = num1;
    this.roll_num2 = num2;
    this.roll_gap = num2 - num1;
    
    this._powerLabelClone.setVisible(true);
    this._powerLabelClone.setString(num1);
    this._powerLabel.setVisible(false);
    
    this.schedule(this.rollFightNumAction, 0.02);
},

rollFightNumAction: function(dt) {
    this.roll_num1 += this.roll_gap*dt/0.4;
    
    if ((this.roll_gap > 0 && this.roll_num1 >= this.roll_num2) || (this.roll_gap < 0 && this.roll_num1 <= this.roll_num2)) {
        this.unschedule(this.rollFightNumAction);
        this._powerLabelClone.setVisible(false);
        this._powerLabel.setVisible(true);
        
        var label_end = this._powerLabel.clone();
        label_end.setString(this.roll_gap < 0 ? this.roll_gap : ("+"+this.roll_gap));
        label_end.setPosition(cc.p(label_end.getPositionX(), label_end.getPositionY()-20));
        this._powerBg.addChild(label_end);
        label_end.runAction(cc.Sequence.create(cc.Spawn.create(cc.FadeOut.create(0.4),cc.MoveBy.create(0.4,cc.p(0,20))),cc.CallFunc.create(function(sender){sender.removeFromParent();})));
    }
    
    this._powerLabelClone.setString(Math.floor(this.roll_num1));
},

实现方式二:
固定时间从数字1滚动到数字2

autoRollNumber: function (start, end) {
    var bEnable = start !== end;
    this._autoRolling = bEnable;
    this._autoRollTick = 0;
    this._autoStarNum = start;
    this._autoNumSpacing = end - start;

    this.unschedule(this._autoRoll);
    this.updateItemNum(start);
    if (bEnable) {
        this.schedule(this._autoRoll);
    }
},

_autoRoll: function (dt) {
    var totalTime = ItemNumAutoRollTotalTime;
    this._autoRollTick += dt;
    if (this._autoRollTick >= totalTime) {
        this._autoRollTick = totalTime;
        this._autoRolling = false;
        this.unschedule(this._autoRoll);
    }
    this.updateItemNum(parseInt(this._autoRollTick * this._autoNumSpacing / totalTime) + this._autoStarNum);
},
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值