cocos2dx lua创建滚动的数字

文章介绍了如何使用Lua编写UINumberScroll类,该类用于创建一个带有动态数值滚动效果的UI节点,包括设置起始和结束数字,以及使用TextAtlas显示数字。UINumberNode负责具体数字的显示和动画效果。
摘要由CSDN通过智能技术生成

自己想用别人的。但是都要积分下载。所以就自己写了个。放上来吧。

UINumberScroll.lua部分。路径自己更改。 Tools.numFormat(tostring(num))就是将数字三位隔一个逗号而已。自行写哈。

--使用说明、
-- local NumScroll = require("src.mainUI.UINumberScroll")
-- local numnode = NumScroll.new()
-- self:addChild(numnode)
-- numnode:setPosition(cc.p(675, 375))
-- numnode:setStartStr(5698)
-- numnode:setEndNum(3826)
local UINumberScroll = class("UINumberScroll", cc.Node)
local UINumberNode = require("src.mainUI.UINumberNode")
local Tools = require("src.framework.Tools")
local eachtime = 0.05
local TextWidth = 21
local TextHeight = 32

function UINumberScroll:ctor()
    self.startNum = nil
    self._startStr = nil
    self.endNum = nil
    self.startNumNode = {}
end

function UINumberScroll:setStartStr(num)
    self.startNum = num
    self._startStr = tostring(Tools.numFormat2(tostring(num)))
    local startStrLen = string.len(self._startStr)

    self._numlayer  = ccui.Layout:create()
    -- self._numlayer:setBackGroundColorType(ccui.LayoutBackGroundColorType.solid)
    -- self._numlayer:setBackGroundColorOpacity(180)
    -- self._numlayer:setBackGroundColor(cc.c4b(255, 255, 255, 255))
    self._numlayer:setClippingEnabled(true)
    self._numlayer:setAnchorPoint(0, 0)
    self._numlayer:setContentSize(cc.size(TextWidth*startStrLen, TextHeight))
    self:addChild(self._numlayer)
    local idnex = 1
    for i = 1, startStrLen, 1 do
        local str = string.sub(self._startStr, i, i)
        local numNode = UINumberNode.new()
        numNode:setStartStr(str)
        numNode.startStr = str
        numNode:setPosition(TextWidth/2+TextWidth*(i - 1), TextHeight/2)
        self._numlayer:addChild(numNode)

        if tonumber(str) ~= nil then
            table.insert(self.startNumNode, idnex, numNode)
            idnex = idnex + 1
        end
    end
end

function UINumberScroll:setEndNum(num)
    self.endNum = num
    local endTextStr = tostring(Tools.numFormat2(tostring(num)))
    local function addZeroForEndStr()
        if string.len(endTextStr) ~= string.len(self._startStr) then
            endTextStr = "0" .. endTextStr
            addZeroForEndStr()
        else
            return
        end
    end
    
    addZeroForEndStr()
    
    local time = 0
    for i = #self.startNumNode, 1, -1 do
        local str = string.sub(endTextStr, i, i)
        if tonumber(str) ~= nil then
            local numSpCount = self:getCountBeteen(tonumber(str), tonumber(self.startNumNode[i].startStr), self.endNum > self.startNum and true or false)
            self.startNumNode[i]:setEndNum(str, self.endNum > self.startNum and true or false, time ,eachtime ,numSpCount)
            time = time + numSpCount * eachtime
        end
    end
end

function UINumberScroll:getCountBeteen(endnum, startnum, isadd)
    local numSpCount = 0
    if endnum == startnum then
        numSpCount = 10
    else
        if isadd then
            if endnum > startnum then
                numSpCount = endnum - startnum
            else
                numSpCount = 10 - (startnum - endnum)
            end
        else
            if endnum < startnum then
                numSpCount = startnum - endnum
            else
                numSpCount = 10 - (endnum - startnum)
            end
        end
    end
    return numSpCount
end

return UINumberScroll

UINumberNode.lua 创建的滚动的节点部分。 字体有的 字体png  

local UINumberNode = class("UINumberNode", cc.Node)
local TextWidth = 21
local TextHeight = 32
local respath = "res_new/honorRooms/,0123456789.png"

function UINumberNode:ctor()
    self.startNum = nil
    self.lableArr = {}
    self.startPosx = 0
end

function UINumberNode:setStartStr(num)
    self.startNum = num
    local goldTextAtlas = ccui.TextAtlas:create(num, respath, TextWidth, TextHeight, ".")
    goldTextAtlas:setPosition(0, 0)
    self:addChild(goldTextAtlas)
    table.insert(self.lableArr, 1, goldTextAtlas)
end

function UINumberNode:setEndNum(num, isAdd, daytime, eachtime, numSpCount)
    local endStr = num
    for i = 1, numSpCount, 1 do
        local str = nil
        if isAdd then
            str = (self.startNum + i)%10
        else
            str = ((self.startNum - i) > 0 and (self.startNum - i) or (10 - (self.startNum - i)))%10
        end
        local goldTextAtlas = ccui.TextAtlas:create(str, respath, TextWidth, TextHeight, ".")
        goldTextAtlas:setPosition(0, TextHeight*i*(isAdd and 1 or -1))
        table.insert(self.lableArr, #self.lableArr + 1, goldTextAtlas)
        self:addChild(goldTextAtlas)
    end

    local posx = self:getPositionX()
    self.startPosx = posx
    local posy = TextHeight * numSpCount * (isAdd and -1 or 1)
    local nodetime = numSpCount * eachtime
    self:runAction(cc.Sequence:create(
        cc.DelayTime:create(daytime),
        cc.EaseSineInOut:create(cc.MoveTo:create(nodetime, cc.p(posx, posy))),
        cc.CallFunc:create(function ()
            self:NumNodeEnd()
        end)
    ))
end

function UINumberNode:NumNodeEnd()
    self.lableArr[1]:setString(self.lableArr[#self.lableArr]:getString())
    self:setPosition(self.startPosx, TextHeight/2)
    for i, v in ipairs(self.lableArr) do
        if i ~= 1 and tolua.isnull(v) then
            v:removeFromParent()
        end
    end
end

return UINumberNode

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值