我正在寫一個棋盤遊戲,我需要檢查玩家的敵人正在移動並提示進行攻擊。然而,由於JavaFX應用程序線程調用了移動方法,因此我希望能夠提示用戶是否想要與敵人戰鬥。JavaFX等待用戶輸入
我的對話框工作正常使用等待和通知哪些不工作在主線程沒有崩潰的程序,沒有人知道如何暫停執行此線程,直到用戶單擊其中一個按鈕。
我很抱歉說明,它的晚。
方法來檢查敵人 的敵人這個方法檢查,如果用戶選擇的是返回的敵人。它在JavaFX線程上運行。
private Ship pathBattle(Vector gridPosition){
//Check if there are ships on the path to the destination
for(Button cell : activeButtons){
//Check the button position is not that of the current button, dont go past the current button
Vector pixPosition = new Vector(cell.getLayoutX(), cell.getLayoutY());
//Convert to a grid referance
Vector gridPos = Vector.pixToGrid(pixPosition);
if(!gridPos.equals(gridPosition)){
//This button is not the destination
//Check for any ships on that cell
Ship collided = Ship.isOccupiedButton(cell);//Returns the ship if there is one on the cell
if(collided != null){
//There is a ship, prompt to battle
boolean battle = UtilPopups.showConfirmationDialog("Do you want to battle " + collided.getName(), "YAR!", "NAY!");
Game.printError("Ship collision");
if(battle){
return collided; //Return the ship to battle
}
}
}
//On to the next button
}
return null;
}
顯示彈出 這樣確實可以在porgram的其他領域,沒有問題
public static boolean showConfirmationDialog(String lblPrompt, String btnYes, String btnNo){
//Check if the confirmation controller is not null
if(confirmationDialog != null){
confirmationDialog.lbl_prompt.setText(lblPrompt);
confirmationDialog.btn_no.setText(btnNo);
confirmationDialog.btn_yes.setText(btnYes);
//Show the base
show(confirmationDialog.base_pane);
//Pause this thread until user input is given from GUI thread
synchronized (confirmationDialog) {
try{
confirmationDialog.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
//Program resumed, user input given and stored in response
if(confirmationDialog.response.equals("yes")){
confirmationDialog.response = null; //Set back to null
return true;
} else {
confirmationDialog.response = null; //Set back to null
return false;
}
}
//Class not initialized
Game.printError("UtilPopups->showConfirmationDialog: Dialog NULL!");
return false;
}
它不是一種方法,它是一個具有Ship(敵人)值的變量,或者如果該被測試的瓦片上不存在Ship,則爲null。 – user2131323