cocos2dx 3.3 Lua 实现倒计时功能

前端之家收集整理的这篇文章主要介绍了cocos2dx 3.3 Lua 实现倒计时功能前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

这里用cocos2dx 3.3 + Lua 实现一个倒计时的功能,游戏中的倒计时是不可或缺的,我把倒计时写成一个lua类,这样调用的时候就方便很多了,这个类还可以有很多的扩展性,这里有两种显示方法如果Countdown.showtype == true ,就显示成 倒计时:00:10:05 格式的,如果Countdown.showtype == false,就显示成这种 00:05后 格式的 但是,如果要同时显示两个倒计时,就不可以了,只能显示一个倒计时。通常倒计时到0时,我们会做一些限制,这里也实现了这个方法,如果倒计时为0时,就调用我们传进去的函数,下面就来看这个倒计时类的使用方法。。

我还引用了这条博客http://www.jb51.cc/article/p-ytfbhpeb-nk.html

下面是一个类文件,Countdown.lua

--写一个倒计时类

local Countdown = class("Countdown")

Countdown.__index = Countdown

Countdown.hour = nil   --小时
Countdown.minute = nil --分钟
Countdown.second = nil --秒钟
Countdown.func = nil
Countdown.showtype = true
function Countdown.create()
    local label = Countdown.new()
    return label
end


function Countdown.ctor()
	Countdown.winsize = cc.Director:getInstance():getWinSize()
	Countdown.scheduler = cc.Director:getInstance():getScheduler()
	Countdown.schedulerID = nil
    print("输出======Countdown.ctor()=========")
    print("倒计时进行中。。。。")
end
--设置倒计时到00:00:00时调用这个函数,传入的参数是一个函数
function Countdown.function_(f)  
    Countdown.func = f
    
end

function Countdown.settime(hour,minute,second)
	Countdown.hour = hour   --小时
	Countdown.minute = minute --分钟
	Countdown.second = second --秒钟
end

function Countdown.gettime()
	return Countdown.hour,Countdown.minute,Countdown.second
end



function Countdown.add_0()
	--将int类型转换为string类型
    Countdown.hour   = tostring(Countdown.hour)
	Countdown.minute = Countdown.minute .. ""
	Countdown.second = Countdown.second .. ""

	--当显示数字为个位数是,前位用补上
	if string.len(Countdown.hour) == 1 then 
   		Countdown.hour = "0" .. Countdown.hour
	end

	if string.len(Countdown.minute) == 1 then
    	Countdown.minute = "0" .. Countdown.minute
	end

	if string.len(Countdown.second) == 1 then
    	Countdown.second = "0" .. Countdown.second
	end
end

--创建一个Label,传入x,y坐标
function Countdown.Showlabel(x,y)
	--Countdown.time如果不等于空,就先remove掉
    if Countdown.time ~= nil then
        Countdown.time:getParent():removeChild(Countdown.time,true)
        Countdown.time = nil
    end
    --创建时间标签用以显示
    Countdown.time = cc.LabelTTF:create("倒计时:" .. Countdown.hour .. ":"
     .. Countdown.minute .. ":" .. Countdown.second,"fonts/Marker Felt.ttf",35)
	Countdown.time:setColor(cc.c3b(255,0))
	Countdown.time:setPosition(cc.p(Countdown.winsize.width/2,Countdown.winsize.height/2))
	return Countdown.time
end




--倒计时更新函数
function Countdown.anticlockwiseUpdate(time)
--    Countdown.second = Countdown.second -1  
--加上tonumber也可以,如下面,不加也可以,lua有这个功能的
    Countdown.second = tonumber(Countdown.second) -1 

    if Countdown.second == -1 then
        if Countdown.minute ~= -1 or Countdown.hour ~= -1 then
            Countdown.minute = Countdown.minute - 1
            Countdown.second = 59
            if Countdown.minute == -1 then
                if Countdown.hour ~= -1 then
                    Countdown.hour = Countdown.hour - 1
                    Countdown.minute = 59
                    
                    if Countdown.hour == -1 then
                        --倒计时结束停止更新
                        if Countdown.schedulerID ~= nil then
                            Countdown.scheduler:unscheduleScriptEntry(Countdown.schedulerID)
                            Countdown.schedulerID = nil
                            
                        end
                        Countdown.second = 0
                        Countdown.minute = 0
                        Countdown.hour = 0
                        Countdown.time:setColor(cc.c3b(0,255,0)) --以绿色标识结束
                        Countdown.func()  --倒计时为0时,调用这个函数
                        
                    end

                end
            end
        end
    end

    Countdown.second = tostring(Countdown.second)
    Countdown.minute = tostring(Countdown.minute)
    Countdown.hour = tostring(Countdown.hour)

    if string.len(Countdown.second) == 1 then
        Countdown.second = "0" .. Countdown.second
    end

    if string.len(Countdown.minute) == 1 then
        Countdown.minute = "0" .. Countdown.minute

    end

    if string.len(Countdown.hour) == 1 then
        Countdown.hour = "0" .. Countdown.hour
    end
    if Countdown.showtype == true then
        Countdown.time:setString("倒计时:" .. Countdown.hour ..":".. Countdown.minute ..":".. Countdown.second)
    elseif Countdown.showtype == false then
        Countdown.time:setString(Countdown.minute ..":".. Countdown.second .. "后")
    end  
end



--倒计时刷新函数
function Countdown.scheduleFunc()
    --隔一秒刷新这个函数
    Countdown.schedulerID = Countdown.scheduler:scheduleScriptFunc(Countdown.anticlockwiseUpdate,1,false)  
end

--移除这个刷新函数
function Countdown.remove_scheduler()
    if Countdown.schedulerID ~= nil then
        Countdown.scheduler:unscheduleScriptEntry(Countdown.schedulerID)
        Countdown.schedulerID = nil

    end
end

--重设
function Countdown.reset(hour,second)
    Countdown.remove_scheduler()    
    Countdown.hour = hour   --小时
    Countdown.minute = minute --分钟
    Countdown.second = second --秒钟
    Countdown.scheduleFunc()
    
end

function Countdown.remove_hour()
    if Countdown.time ~= nil then    
        if tonumber(Countdown.hour) == 0 then
            --设为分钟:秒 如09:11
            Countdown.time:setString(Countdown.minute ..":".. Countdown.second .. "后") 
            Countdown.showtype = false
        end
    end   
    
end

return Countdown


有了上面这个倒计时类文件之后,下面我们就来用一下:

    local mainLayer = cc.Layer:create()
    local Countdown_a = require("src/Countdown")  --将倒计时类赋给一个值
    Countdown_a.remove_scheduler()               --如果有刷新,就先删除,赋值为nil
    Countdown_a.ctor()
    Countdown_a.settime(0,5)                   --设置时间: 时钟,分钟,秒钟
    Countdown_a.add_0()                          --将传入的时间,全部转成string类型的
    local tt = Countdown_a.Showlabel(1,1)        --传入x,y坐标值,创建一个label显示
    Countdown_a.scheduleFunc()                   --创建一个刷新函数
    local function call_f()             --这里是倒计时结束后,要调用函数
        print("----倒计时为0时,即结束了,调用这个函数call_f()-----") 
    end
    
    Countdown_a.function_(call_f)                --我们把函数作为参数,传到这个类中
    mainLayer:addChild(tt,3)            --把label 添加到层中


经过上面的代码,一个倒计时就创建成功了,如下图:

倒计时中.........


倒计时结束后!!!!





这里是第二种显示:00:05后

    local layer = cc.Layer:create()
    local Countdown_b = require("src/Countdown")  --将倒计时类赋给一个值
    Countdown_b.remove_scheduler()                --如果有刷新,就先删除,赋值为nil
    Countdown_b.ctor()
    Countdown_b.settime(0,9)              --设置时间: 时钟,分钟,秒钟
    Countdown_b.add_0()                     --将传入的时间,全部转成string类型的
    local ttf = Countdown_b.Showlabel(1,1)  --传入x,y坐标值,创建一个label显示
    ttf:setScale(2)                         --扩大两倍      
    Countdown_b.remove_hour()               --移除时钟的显示
    Countdown_b.scheduleFunc()              --创建一个刷新函数
    local function call_f()                 --这里是倒计时结束后,要调用函数
        print("----倒计时为0时,即结束了,调用这个函数call_f()-----") 
    end
    
    Countdown_b.function_(call_f)    --我们把函数作为参数,传到这个类中
    layer:addChild(ttf,3)            --把label 添加到层中

倒计时中.........



倒计时结束后!!!

原文链接:/cocos2dx/345043.html

猜你在找的Cocos2d-x相关文章