加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

【cocos2dx 3.3 lua】05 环绕倒计时效果

发布时间:2020-12-14 17:09:01 所属栏目:百科 来源:网络整理
导读:一个环绕倒计时效果,直接上代码: --[[倒计时类start 开始倒计时stop 终止倒计时--]]local CountDown = class("CountDown",function() return cc.Node:create()end)CountDown._Timer = nilCountDown._Start = 0CountDown._End = 100CountDown._Duration = 0

一个环绕倒计时效果,直接上代码:

--[[
倒计时类
start 开始倒计时
stop  终止倒计时
--]]


local CountDown = class("CountDown",function()
    return cc.Node:create()
end)

CountDown._Timer = nil
CountDown._Start = 0
CountDown._End = 100
CountDown._Duration = 0
CountDown._EndCallback = nil

--[[
resSprite   倒计时资源
duration    倒计时时间,默认0
st          开始百分比,默认0
ed          结束百分比,默认100
endCallback 回调,stop时调用
--]]
function CountDown:create(resSprite,duration,st,ed,endCallback)
    return CountDown.new(resSprite,endCallback)
end

function CountDown:ctor(resSprite,endCallback)
    if st ~= nil then
        self._Start = st
    end
    if ed ~= nil then
        self._End = ed
    end

    self._Timer = cc.ProgressTimer:create(resSprite)
    self._Timer:setType(cc.PROGRESS_TIMER_TYPE_RADIAL)
    self._Timer:setPosition(cc.p(0,0))
    -- 0-->100 动画方向 逆时针
    self._Timer:setReverseDirection(true)
    -- 设置中心点
    -- self._Timer:setMidpoint(cc.p(0.25,0.5))
    self._Timer:setVisible(false)

    self:addChild(self._Timer)

    self._Duration = duration
    self._EndCallback = endCallback
end

function CountDown:start( time )
    if time == nil then
        time = self._Duration
    end

    self:stop(false,false)

    self._Timer:runAction( cc.Sequence:create(
        cc.CallFunc:create(
            function( sender )
                self._Timer:setVisible(true)
            end),cc.ProgressFromTo:create(time,self._Start,self._End),cc.CallFunc:create(
            function( sender )
                self:stop()
            end) ) )
end

function CountDown:stop( isHide,isCallback )
    if isHide == nil then
        isHide = true
    end
    if isCallback == nil then
        isCallback = true
    end

    self._Timer:stopAllActions()
    if isHide then    
        self._Timer:setVisible(false)
    end

    if isCallback and self._EndCallback ~= nil then
        self._EndCallback()
    end
end

return CountDown


调用示例:
    local timer = cc.Sprite:create("test.png")
    
    CountDown = require("CountDown")
    local cd = CountDown:create(timer,10,100,function (  )
        print("CountDown end!=========")
    end)
    cd:setPosition(cc.p(250,400))
    rootLayer:addChild(cd)
    cd:start()


效果图:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读