我不确定我是否误解了coroutine的使用,但这是我的代码:
talk = coroutine.create(function ()
print("I am speaking within 60 seconds")
end)
time = coroutine.create(function ()
if coroutine.status(talk) == running then
for i = 60, 0, -1 do
print(i)
end
sleep(1)
else
coroutine.resume(talk)
end
end)
coroutine.resume(time)
所有打印的是我在60秒内发言,我期待它在倒计时内说出来。我该如何解决这个问题?
答案 0 :(得分:0)
talk = coroutine.create(function ()
print("I am speaking within 60 seconds")
coroutine.resume(time)
end)
time = coroutine.create(function ()
for i = 60, 0, -1 do
print(i)
coroutine.resume(talk)
end
end)
coroutine.resume(time)
答案 1 :(得分:0)
talk = coroutine.create(function ()
print("I am speaking within 60 seconds")
coroutine.yield()
end)
time = coroutine.create(function ()
for i = 60, 0, -1 do
coroutine.resume(talk)
print(i)
end
end)
coroutine.resume(time)
答案 2 :(得分:0)
你正在呼叫睡眠,但我没有看到某个地方声明了这个功能。这是一个已实现sleep()的修改版本:
local function sleep(time)
local sleep_until = os.time() + time
repeat until os.time() >= sleep_until
end
local time, talk
talk = coroutine.create(function ()
print("I am speaking in 60 seconds")
coroutine.resume(time)
end)
time = coroutine.create(function ()
while true do
for i = 60, 0, -1 do
print(i)
sleep(1)
end
coroutine.resume(talk)
end
end)
coroutine.resume(time)