2014-10-30 52 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在相當長一段時間。