2017-05-02 120 views
0

我正在寫一個棋盤遊戲,我需要檢查玩家的敵人正在移動並提示進行攻擊。然而,由於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; 
} 

回答

0

有關顯示對話框中阻止任何程序線程,例如:

static private void showAndBlock(Dialog dialog) { 
    if (Platform.isFxApplicationThread()) { 
     dialog.showAndWait(); 
    } else { 
     CountDownLatch lock = new CountDownLatch(1); 
     Platform.runLater(() -> { 
      dialog.showAndWait(); 
      lock.countDown(); 
     }); 
     try { 
      lock.await(); 
     } catch (InterruptedException e) { 
      // Just in case you call yourTherad.interrupt(), 
      // the thread will be joined forcibly through here. 
     } 
    } 
} 

但我不知道它是否適用於你的線程設計。

0

的碰撞方法字面上犯規什麼的代碼,它不要求一個輸入所以它不能工作。

我想,遺憾沒有良好的英語

+0

它不是一種方法,它是一個具有Ship(敵人)值的變量,或者如果該被測試的瓦片上不存在Ship,則爲null。 – user2131323