cocoslua代码滚动数字

麹鸿煊
2023-12-01
function RoomItem.createScrollNum(parent)
    local layer = ccui.Layout:create()
    local size = parent:getContentSize()
    -- layer:setBackGroundColor(cc.c3b(0, 255, 0))
    local width = 28
    local height = 33
    layer:setContentSize(width, height)
    layer:setAnchorPoint(0.5, 0.5)
    layer:setClippingEnabled(true)
    layer:setName("SCROLL_NODE")
    -- 添加遮罩:
    parent:addChild(layer)   
    layer:setPosition(size.width - 15, size.height / 2 + 12) 

    local font = "game/ax/oxWholeCompare/res/0fnt/tbnn_label_ccjj.fnt"
    local label1 = ccui.TextBMFont:create()
    label1:setFntFile(font)

    layer:addChild(label1)
    label1:setPosition(width/2, height/2)

    local font = "game/ax/oxWholeCompare/res/0fnt/tbnn_label_ccjj.fnt"
    local label2 = ccui.TextBMFont:create()
    label2:setFntFile(font)
    layer:addChild(label2)
    label2:setPosition(width / 2, height / 2)

    function layer.stopScroll()
        layer:stopAllActions()
        -- 计算当前的位置
    end

    -- 滚动
    -- @param waitTime 每个滚动间隔时间
    -- @param callback  回调
    function layer:setNumByScroll(waitTime, callback)
        layer.stopScroll()
        -- 计算现在的位置, 需要滚动多少个单位,每个滚动间隔时间
        -- 间隔多少秒转动一次
        local subtime = 0.07
        local minDelay = 0.14
        local delaytime = waitTime - subtime < minDelay and minDelay or waitTime - subtime
        local minSub = 0.07
        local delay = cc.DelayTime:create(delaytime)
        local runNode = label1
        local func = cc.CallFunc:create(function()
            label1:stopAllActions()
            label2:stopAllActions()
            local seq1 = cc.MoveBy:create(subtime, cc.p(0, height))
            local seq2 = cc.MoveBy:create(subtime, cc.p(0, height))
            runNode = runNode == label1 and label2 or label1
            local otherNode = runNode == label1 and label2 or label1
            runNode:setPositionY(height/2)
            otherNode:setPositionY(-0.5 * height)
            local afterNum = runNode.m_number + 1 > 9 and 0 or runNode.m_number + 1
            otherNode:setString(afterNum)
            otherNode.m_number = afterNum
            runNode:runAction(cc.Sequence:create(cc.CallFunc:create(function()
                runNode:setOpacity(185)
            end), seq1, cc.CallFunc:create(function()
                runNode:setOpacity(255)
                if runNode.m_number == 9 then
                    -- 往前面加数字
                    if callback then callback() end
                end
            end)))
            otherNode:runAction(seq2)
        end)
        local rep = cc.Sequence:create(delay, func)
        layer:runAction(cc.RepeatForever:create(rep))
    end
    -- 静态
    -- @param num  0-9的值
    function layer:initNum(num)
        layer.stopScroll()
        label1:stopAllActions()
        label2:stopAllActions()
        label1:setPositionY(height / 2)
        label2:setPositionY(-0.5 * height)
        num = tonumber(num)
        label1.m_number = num
        local afternum = num + 1 > 9 and 0 or num + 1 
        label2.m_number = afternum
        label1:setString(label1.m_number)
        label2:setString(label2.m_number)
    end

    function layer:setNumberByStatic(num)

    end

    layer:initNum(0)

    return layer
end

 类似资料: