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

你可能感兴趣的:(cocos,算法,cocos-creator,cocos,creator,typescript,前端)