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)
}
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;
}
cancelTimer(target, callback) {
var Timer = cc.director.getScheduler();
if (!Timer.isScheduled(callback, target)) {
return;
}
Timer.unschedule(callback, target);
}