码迷,mamicode.com
首页 > 其他好文 > 详细

ulua/tolua中timer.lua和event.lua的使用(Luaframework)

时间:2017-12-25 15:05:26      阅读:1451      评论:0      收藏:0      [点我收藏+]

标签:stop   过程   listener   upd   color   date   ddl   update   方式   

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,
    --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

 

ulua/tolua中timer.lua和event.lua的使用(Luaframework)

标签:stop   过程   listener   upd   color   date   ddl   update   方式   

原文地址:http://www.cnblogs.com/vsirWaiter/p/8108888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!