|
楼主 |
发表于 2011-1-18 17:17:17
|
显示全部楼层
应楼上要求贴出来 wait 代码
-- 线程表,储存正在暂停的线程
local wait_table = {}
--setmetatable(wait_table, {__mode = "v"}) --weak table
-- 被定时器调用以恢复一个暂停的线程
function wait_timer_resume(name)
--print(table.getn(wait_table))
print("wait_id: ",name)
thread = wait_table[name]
if thread~=nil then
assert(coroutine.resume (thread))
else
print(name," 不存在")
end -- if
wait_table[name]={} --垃圾回收
end -- function wait_timer_resume
-- 在脚本中调用这个函数来暂停当前的线程
function wait(address, seconds)
id = "wait_timer_" .. GetUniqueNumber ()
hours = math.floor(seconds / 3600)
seconds = seconds - (hours * 3600)
minutes = math.floor(seconds / 60)
seconds = seconds - (minutes * 60)
print("等待",seconds,"s"," ",id)
wait_table[id] =coroutine.create(function()
print("执行")
address()
end)
status = AddTimer (id, hours, minutes, seconds, "",
timer_flag.Enabled + timer_flag.OneShot +
timer_flag.Temporary + timer_flag.Replace,
"wait_timer_resume")
assert(status == error_code.eOK, error_desc[status])
end -- function wait
--[[function test()
print("hello world")
end
wait(test,3)]]
--[[local function wait(s)
print("等待",s)
world.AddTimer ("delay", 0, 0, s, "process.resume()", timer_flag.OneShot+timer_flag.Replace , "")
world.SetTimerOption ("delay", "send_to", "12")
world.SetTimerOption ("delay", "group", "process")
world.EnableTimer("delay",true)
world.ResetTimer("delay")
end]] |
|