ulua/tolua中timer.lua和event.lua的使用(Luaframework)
Timer.lua
Timer計時器:
local tim = nil
local count = 0
function Game.FunTest() count = count + 1 print(0,0,0,count) if count > 4 then tim:Stop() --中止當(dāng)前的計時器 end end --初始化完成,發(fā)送鏈接服務(wù)器信息-- function Game.OnInitOK() tim = Timer.New(Game.FunTest,1, -1, true) --參數(shù)1為調(diào)用的方法名,參數(shù)2為間隔時間,參數(shù)3為循環(huán)次數(shù)(當(dāng)為-1時無限循環(huán)),參數(shù)4為是否忽略時間的Scale,scale false 采用deltaTime計時,true 采用 unscaledDeltaTime計時 --tim:Start() tim:Start() end
FrameTimer計時器:
tim = FrameTimer.New(Game.FunTest,100, 5) --和上面使用的一樣,但參數(shù)不一樣,參數(shù)1為方法名,參數(shù)2和參數(shù)3組合起來的意思是在100幀內(nèi)執(zhí)行5次方法。 tim:Start()
CoTimer計時器:
tim = CoTimer.New(Game.FunTest,1, 5) --和上面使用的一樣,但參數(shù)不一樣,參數(shù)1為方法名,參數(shù)2為兩次執(zhí)行的間隔時間,參數(shù)3為執(zhí)行次數(shù)(參數(shù)3為-1時無限次數(shù))
tim:Start()
event.lua
UpdateBeat = event("Update", true) --邏輯的Update
LateUpdateBeat = event("LateUpdate", true) --延遲的update
FixedUpdateBeat = event("FixedUpdate", true) --物理的update
CoUpdateBeat = event("CoUpdate") --協(xié)程的每一幀更新
調(diào)用方式:
local count = 0
function Game.FunTest(f1)
count = count + 1
print(f1,count)
end
function Game.OnInitOK()
local parm = 0 local handle = UpdateBeat:CreateListener(Game.FunTest, parm) --好像只支持一個參數(shù) UpdateBeat:AddListener(handle)
local handle = LateUpdateBeat:CreateListener(Game.FunTest, parm) --好像只支持一個參數(shù) LateUpdateBeat:AddListener(handle)
local handle = FixedUpdateBeat:CreateListener(Game.FunTest, parm) --好像只支持一個參數(shù) FixedUpdateBeat:AddListener(handle)
local handle = CoUpdateBeat:CreateListener(Game.FunTest, parm) --好像只支持一個參數(shù)
CoUpdateBeat:AddListener(handle)
end
event.lua使用FixedUpdateBeat的過程中移除FixedUpdateBeat:
local count = 0 local handle = nil function Game.FunTest(f1) count = count + 1 print(f1,count) if count > 10 then FixedUpdateBeat:RemoveListener(handle) end end --初始化完成,發(fā)送鏈接服務(wù)器信息-- function Game.OnInitOK() local parm = 0 handle = FixedUpdateBeat:CreateListener(Game.FunTest, parm) --好像只支持一個參數(shù) FixedUpdateBeat:AddListener(handle) end
小時候我把老婆種到地下,長大了我能收獲一大堆老婆!
我是威少,我是一名Unity游戲的主程,我為自己帶鹽,希望此文能給您一點點微不足道的幫助,祝你成功!

浙公網(wǎng)安備 33010602011771號