【cocos creator】金额滚动效果

    let RichText = this.label_red_cash.getComponent(cc.Label);
    let str = RichText.string;
    this.randCashLabel(RichText, Number(str), 1000);

    fixForce(count, fixTo): string {
        let a = (count + "").split(".");
        let b = a[0];
        if (a.length > 1) b = a[0] + "." + a[1].slice(0, fixTo);
        if (b == "0.00" && count != 0) {
            if (a.length > 1) b = a[0] + "." + a[1].slice(0, 4);
        }
        return b;
    }

    randCashLabel(label: cc.Label | cc.RichText, currentCashNum: number, totalCash: number, desc = "[1]", cb?, t = 0.01) {
        let showNum = currentCashNum;
        let count = 0
        let temp = Math.max(0, (totalCash - currentCashNum));
        cc.log(showNum)
        let timmer = () => {
            count++;
            let random = this.getRandomNum(temp / 20, temp / 15);
            if ((currentCashNum + "").split(".").length > 1 || (totalCash + "").split(".").length > 1) {
                showNum += random
            }
            else {
                let num = Math.floor(random)
                showNum += num;
            }
            if (showNum >= totalCash || count == 20) {
                showNum = totalCash;
                this.cancelTimer(label.node, timmer);
                cb && cb();
            }
            label.string = desc.replace("[1]", this.fixForce(showNum, 2) + "");
            cc.log(showNum)
        }
        this.cancelTimer(label.node, timmer);
        this.setTimer(label.node, timmer, t, 20)
    }

    /**
     * 设置一个计时器 
     * @param {*} target 通常是当前脚本或者节点 object类型
     * @param {*} callback 回调函数
     * @param {*} interval 执行间隔  
     * @param {*} repeatNum 执行次数  可选,默认为无限次
     * @param {*} delay 延迟时间 可选,默认0
     */
    setTimer(target, callback, interval, repeatNum?, delay?) {
        //必须参数检查
        if (!target) {
            console.error("没有设置计时器目标!");
            return;
        }
        if (!callback) {
            console.error("没有回调函数!");
            return;
        }
        if (interval == undefined) {
            console.error("没有设置执行间隔!");
            return;
        }
        //默认参数设置
        if (repeatNum == undefined || repeatNum == null) {
            repeatNum = cc.macro.REPEAT_FOREVER;
        } else {
            repeatNum = repeatNum > 0 ? repeatNum - 1 : 0;
        }
        delay = delay || 0;
        //设置计时器
        let Timer = cc.director.getScheduler();
        Timer.enableForTarget(target);
        Timer.schedule(callback, target, interval, repeatNum, delay, false);
        return true;
    }
    /**
     * 取消一个计时器
     * @param {*} target 通常是当前脚本或者节点 object类型  
     * @param {*} callback 回调函数
     */
    cancelTimer(target, callback) {
        var Timer = cc.director.getScheduler();
        if (!Timer.isScheduled(callback, target)) {
            //console.warn("不存在的计时器", target, callback)
            return;
        }
        Timer.unschedule(callback, target);
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烧仙草奶茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值