所以我得到了使用JavaFX構建的這款遊戲,並且我在使用gui時遇到了一些問題。當用戶點擊一個按鈕時,會觸發事件處理程序。這是該事件處理程序psudo代碼:Java FX僅在事件處理程序完成時更新GUI
When button is clicked
try to make a move
update the board with the new move
let the AI make a move
update the board with the ai's move
這裏的問題是,最後一行已執行後的板只更新,所以你從來沒有真正看到了人的球員後,董事會已使他或她的。我讀過一些關於做gui的東西
Platform.runLater(() -> {
});
但這似乎不適用於我。我在這裏錯過了什麼?
編輯這是我的代碼:
@FXML
public void btnCell(ActionEvent actionEvent) {
disableAllButtonsDuringAITurn(board.getCells());
Node sourceNode = (Node) actionEvent.getSource();
String cellId = sourceNode.getId();
int row = Character.getNumericValue(cellId.charAt(3));
int col = Character.getNumericValue(cellId.charAt(4));
board = treeBuilder.getFlippedBoard(board, row, col, Cell.Black, Cell.White);
playTurn();
}
private void playTurn() {
updateView(board.getCells());
checkWinner();
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
MakeAIMove();
updateView(board.getCells());
updateAvailableMovesForHuman();
checkWinner();
if (!canHumanMakeTurn) {
playTurn();
}
}//Only here is the view updated.
你如何期望它從用戶的角度來看?用戶應該看到什麼?是否包含任何動畫?人工智能需要多長時間來計算移動? – jewelsea
AI可能會在沒有任何用戶輸入的情況下進行移動,從用戶的角度來看可能實際上是瞬間的。所以,在用戶完成所有輸入後,系統可以立即執行AI移動的計算,並顯示包括兩個玩家移動的棋盤結果。 – jewelsea
爲了防止出現這種情況,您可以在用戶進行移動並更新主板之後阻止用戶輸入,然後運行PauseTransition來安排仿真延遲(或者如果您想要關閉JavaFX應用程序線程的計算,則運行任務)以及暫停或任務完成後,更新董事會以反映人工智能移動。效果將會是用戶做出動作,更新用戶界面,暫停發生,在此期間用戶無法做出新的動作,人工智能進行移動並更新板,之後用戶可以進行新的移動。那是你要的嗎? – jewelsea