2014-12-11 52 views
1

我想實現一個名爲Piece>>userMovesFor:seconds的方法,在「秒」時間內獲取鍵盤輸入並根據它,向現有對象發送一些消息以使它們進行一些計算(我的意思是它們不分支未來的執行流程)。在Pharo v3中等待

因此,從我所瞭解的系統中,我認爲該方法可以是這樣的示意圖。

Piece>>userMovesFor:seconds 
| keyboardInterfacer | 
keyboardInterfacer:=KeystrokeInterpreter on:self. 
keyboardInterfacer takeKeyboardFocus . 
self sleep:40 seconds. 
keyboardInterfacer releaseKeyboardFocus . 

KeystrokeInterpreter將是Morph一個子類。雖然重點是通過Morph>>handleKeystroke來處理按鍵。

Piece>>sleep:seconds只會等待「秒」的時間。

因此,在執行的某個時刻,Piece實例將處於無所事事狀態,而Morph實例處理鍵盤輸入。

我的問題是兩個。

1)有沒有辦法實現Piece>>sleep這樣不會導致系統凍結(因此KeystrokeInterpreter實例可以完成他的工作)?

我試過Duration>>wait,並且自己也實現了自我呼叫等待消息,但是這兩種選擇都凍結了圖像中所有對象的執行。

2)有沒有更好的辦法暗示Piece>>userMovesFor

+1

谷歌我沒有併發事物和事件處理一個職業球員,但你可以參閱http:// pillarhub .pharocloud.com /集線器/ UKO/concurrentProgrammingInPharo並嘗試'keyboardInterfacer takeKeyboardFocus'在岔路口,說不定就會有一定的效果。 – Uko 2014-12-11 07:14:05

回答

2

由於您使用的是Morphic,#step將是您最好的選擇。你可以在Morph上實現它,以及#stepTime(步驟頻率)。只要你的Morph在世界上開放,步驟就會繼續射擊。也許跟蹤Morph中的超時也是最容易的,並且從Piece提供一個回調來控制回傳。

1

添加到肖恩的想法,請確保你看看Morph >> startStepping和Morph中的整個步進和演示者協議。

stepping and presenter protocol

我會做的是避免耦合鍵盤輸入和變種。

只有變形步驟和獨立閱讀鍵盤事件。從那裏,創建Pieces正在偵聽的通知。

使用您的遊戲作爲播音員。

所以,你沒有處理按鍵,而是真正的遊戲相關的事件。

HTH

3

40秒似乎按鍵之間的很長一段時間:)我還沒有研究按鍵處理之前,但是這讓我感興趣讓我戳周圍。也許您正在尋找這樣的事情...

Morph subclass: #Piece 
    instanceVariableNames: '' 
    classVariableNames: '' 
    category: 'Play' 

Piece>>initialize 
    super initialize. 
    self extent: [email protected] 
    "Piece new openInWorld" 

Piece>>handlesMouseOver: evt 
    ^true 

Piece>>mouseEnter: event 
    super mouseEnter: event. 
    self color: Color yellow. 
    self takeKeyboardFocus. 

Piece>>mouseLeave: event 
    super mouseEnter: event. 
    self color: Color blue. 

Piece>>handlesKeyboard: evt 
    ^true 

Piece>>keyStroke: event 
    event keyCharacter == $r ifTrue:[ self color: Color red ]. 
    event keyCharacter == $g ifTrue:[ self color: Color green ]. 
    event keyCharacter == $x ifTrue:[ self delete ]. 
    event keyCharacter == $c ifTrue: 
    [ 
     [ self inform: 'Starting compute without blocking the UI'. 
      (Delay forSeconds: 10) wait. "simulate work" 
      self inform: 'Here are the results'. 
     ] fork. 
    ]. 

獲取更多信息......看看別人是怎麼通過在圖片的上面實現的方法發件人搜索做到這一點。如果您喜歡冒險,在其中一些實現中設置「自停」,然後使用「系統>啓用暫停/檢查一次」。

另一個線程可以找到搜索「[菲羅用戶]例如自定義鼠標事件」 http://lists.pharo.org/pipermail/pharo-users_lists.pharo.org/2014-August/013156.htm