工作生活

ulua/tolua中timer.lua和event.lua的使

2019-07-04  本文已影响0人  Lennie_S

Timer.lua

Timer计时器:

local tim = nil

local count = 0

function Game.FunTest()

    count = count +1    print(0,0,0,count)

    ifcount >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 =0local handle = nil

function Game.FunTest(f1)

    count = count +1    print(f1,count)

    ifcount >10 then

      FixedUpdateBeat:RemoveListener(handle)

    end

end--初始化完成,发送链接服务器信息--function Game.OnInitOK()

    local parm =0    handle = FixedUpdateBeat:CreateListener(Game.FunTest, parm) --好像只支持一个参数

    FixedUpdateBeat:AddListener(handle)

end

上一篇 下一篇

猜你喜欢

热点阅读