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() --中止當前的計時器 end end --初始化完成,發送鏈接服務器信息-- function Game.OnInitOK() tim = Timer.New(Game.FunTest,1, -1, true) --參數1為調用的方法名,參數2為間隔時間,參數3為循環次數(當為-1時無限循環),參數4為是否忽略時間的Scale,scale false 采用deltaTime計時,true 采用 unscaledDeltaTime計時 --tim:Start() tim:Start() end
FrameTimer計時器:
tim = FrameTimer.New(Game.FunTest,100, 5) --和上面使用的一樣,但參數不一樣,參數1為方法名,參數2和參數3組合起來的意思是在100幀內執行5次方法。 tim:Start()
CoTimer計時器:
tim = CoTimer.New(Game.FunTest,1, 5) --和上面使用的一樣,但參數不一樣,參數1為方法名,參數2為兩次執行的間隔時間,參數3為執行次數(參數3為-1時無限次數)
tim:Start()
event.lua
UpdateBeat = event("Update", true) --邏輯的Update
LateUpdateBeat = event("LateUpdate", true) --延遲的update
FixedUpdateBeat = event("FixedUpdate", true) --物理的update
CoUpdateBeat = event("CoUpdate") --協程的每一幀更新
調用方式:
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) --好像只支持一個參數 UpdateBeat:AddListener(handle)
local handle = LateUpdateBeat:CreateListener(Game.FunTest, parm) --好像只支持一個參數 LateUpdateBeat:AddListener(handle)
local handle = FixedUpdateBeat:CreateListener(Game.FunTest, parm) --好像只支持一個參數 FixedUpdateBeat:AddListener(handle)
local handle = CoUpdateBeat:CreateListener(Game.FunTest, parm) --好像只支持一個參數 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 --初始化完成,發送鏈接服務器信息-- function Game.OnInitOK() local parm = 0 handle = FixedUpdateBeat:CreateListener(Game.FunTest, parm) --好像只支持一個參數 FixedUpdateBeat:AddListener(handle) end