function PetInfoPanel:runAddAction(node, target, index)
if not index then
index = 1
end
assert(node)
if not node.getString then
return
end
-- 查找小数点
local str = node:getString()
local _, _, targetNum = string.find(target, "(%d+.%d+)")
-- targetNum
if targetNum == nil then
-- print("目标字符串不含 小数点")
_, _, targetNum = string.find(target, "(%d+)")
end
-- 查找当前显示字符串
-- "-123.261&" 匹配结果为123.261 但是匹配不到"9%"中的9
local _, _, num = string.find(str, "(%d+.%d+)")
-- targetNum
if num == nil then
-- print("当前不含 小数点")
_, _, num = string.find(str, "(%d+)")
end
print("target", target, "targetNum", targetNum, "str", str, "", "num", num)
num = tonumber(num)
targetNum = tonumber(targetNum)
local xishu = 0.3
local sub = num < targetNum and (targetNum-num) * xishu < 1 and 1 or math.floor((targetNum-num) * xishu)
or (targetNum-num) * xishu > -1 and -1 or math.floor((targetNum-num) * xishu)
local temp = num + sub
if sub > 0 then
if temp >= targetNum then
temp = targetNum
end
else
if temp <= targetNum then
temp = targetNum
end
end
local str1 = string.gsub(str, num, temp)
node:runAction(cc.Sequence:create(cc.DelayTime:create(0.05), cc.CallFunc:create(function( ... )
node:setString(str1)
if temp ~= targetNum then
self:runAddAction(node, targetNum, index+1)
end
end)))
end
递归循环调用
利用lua string库实现字符串中的数字变动效果
最新推荐文章于 2023-09-18 16:45:51 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)