2014-10-30 46 views
0

我试图创建一个while true do循环,即反应点击,使用os.pullEvent,并且还更新监视器。如何让os.pullEvent不屈服?

问题是,当我按下其中一个屏幕按钮时,它只会更新屏幕,我发现这是因为pullEvent停止脚本,直到事件被触发。

是否有可能做起来很pullEvent不会阻止我更新显示器呢?

function getClick() 
    event,side,x,y = os.pullEvent("monitor_touch") 
    button.checkxy(x,y) 
end 

local tmp = 0; 

while true do 
    button.label(2, 2, "Test "..tmp) 
    button.screen() 
    tmp++ 
    getClick() 
end 

回答

4

你可以很容易地使用并行API来同时运行两个代码。它的工作原理是它运行它们的序列,直到遇到一些使用os.pullEvent然后交换过来,做另一边,如果两个停在东西做os.pullEvent那么它保持之间交换,直到一个收益率,并从那里继续。

local function getClick() 
    local event,side,x,y = os.pullEvent("monitor_touch") 
    buttoncheckxy(x,y) 
end 

local tmp = 0 
local function makeButtons() 
    while true do 
    button.label(2,2,"Test "..tmp) 
    button.screen() 
    tmp++ 
    sleep(0) 
    end 
end 
parallel.waitForAny(getClick,makeButtons) 

现在,如果你注意到了,第一件事,我做你的while循环到一个功能,并添加了睡眠里面,所以它产生并允许程序切换。最后你会看到parallel.waitForAny(),它运行指定的两个函数,当它们中的一个完成时,在这种情况下,当你点击一个按钮时,它就结束了。但是请注意,在我没有调用这些函数的论点中,我只是通过了它们。

+0

我会试一试,当我得到,谢谢:)。我在看协同程序,但根本无法解决它。 – 2014-10-31 10:00:40

0

我现在没有电脑设备,或查找功能,但我知道你可以使用函数os.startTimer(t),它会在t秒内产生一个事件(我认为它是秒)

用法:

update_rate = 1 

local _timer = os.startTimer(update_rate) 

while true do 
    local event = os.pullEvent() 

    if event == _timer then 
     --updte_screen() 
     _timer = os.startTimer(update_rate) 
    elseif event == --some oter events you want to take action for 
     --action() 
    end 
end 

注:该代码没有进行测试,因此恳求纠正我,如果我做了一个错误,我没有使用computercraft在相当长一段时间。