0

I'm developing a simple game where the user can select a card, and then he has to select the parameters needed by that card.

For Example: I want to use a card, I select it and then I select the parameters needed by that card (another player to decrease his points).

I have created an EventHandler with FXML for the selected card (it's a simple button):

@FXML
private void selectedCard(ActionEvent event){
    System.out.print("Card selected!");
    //Wait here to get the parameters
    //Code using those parameters
}

and another EventHandler for the player selected (which is also a simple button):

 @FXML
private void selectedPlayer(ActionEvent event){
    System.out.print("Player selected!");
    idPlayer = event.getSource.getId();
    }

I would like to wait the user to select the player and then keep going with the code inside selectedCard, how can i do that?

Mattia Surricchio
  • 1,362
  • 2
  • 21
  • 49
  • can the user hit the player button before selecting the card? if not. just set up some state in the `selectedCard()` [save the card id or name enable and display buttons ] method and then check for that state in `selectedPlayer()` – mavriksc Jun 11 '18 at 21:55
  • Unfortunately you have to first select the card, then the parameters. That's because they could be more than one, this was just an example! The card could need 4 players and a dice to be selected (for example) – Mattia Surricchio Jun 11 '18 at 21:58
  • 1
    that is what i am saying. when the player taps the card, persist what card they selected and then enable ui for selecting the player. when the player is selected. then execute your game logic. if that logic lives in the card then you'll have to store a reference to it. and call like card.execute() from selectedPlayer – mavriksc Jun 11 '18 at 22:03
  • 1
    In java 9+ you can use this approach: https://stackoverflow.com/a/46369047/2991525 You could also save the state in a field and handle the player selection based on that info. Depending how many different actions require player selection, using a object containing the logic to handle the player selection could be set from `selectCard` and used from `selectPlayer` (e.g. `Consumer`) – fabian Jun 11 '18 at 23:07
  • So from what i understood, the best approach would be to use 2 different (or more) action event, save the result of each one in a parameter (for example which players i selected) and then use them in my card? (while checking if the number of chosen players is the same required from the card) – Mattia Surricchio Jun 12 '18 at 11:03
  • 1
    you should have a gameController that knows about the cards and the possible targets(players). when it's player A's turn he should only be able to tap a card. the controller detects what card was tapped and ask it for its required number of targets and their type. you can then enable all possible targets to be tapped. each time one is tapped you check if you've met the required number of targets. if so perform the cards action. – mavriksc Jun 12 '18 at 14:57

0 Answers0