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

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

--[[
倒计时类
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()


效果图:

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...