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() --中止当前的计时器
    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

 

posted @ 2017-12-25 12:58  威少小二orz  阅读(4594)  评论(1编辑  收藏  举报