-1

I'm coding a game and I'm trying to get the value of a dice roll from a button click in my UI.

I'm not too familiar with the MVC principles but I heard it's the way to go. Any tips on how to get there from this code ?

int move = -1;
while (move == -1) {
    this.btnLancerLeD.addMouseListener(new MouseAdapter() {

        public int diceRoll;

        @Override
        public void mouseClicked(MouseEvent arg0) {
            diceRoll = jeu.getDe().lancer();
            move = diceRoll;
        }
    });
}

I'm trying to get diceRoll value to move my players on the board.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    You should only call `addMoustListener()` once rather than doing it repeatedly in a while loop. – Code-Apprentice May 17 '19 at 16:58
  • 1
    Why are you using a `MouseListener`? `btnLancerLeD` sounds to me like it's a `JButton`, you should use an `ActionListener` instead. – Frakcool May 17 '19 at 17:00
  • @Code-Apprentice Oh ok, I was trying to get the program to wait for the player to click the "Roll the dice" button before continuing.. – Fouldon May 17 '19 at 17:00
  • @Frakcool I've used WindowBuilder from Eclipse to build the general UI, and this is the method it used when I set the "click Listener". Is ActionListener more convenient ? – Fouldon May 17 '19 at 17:02
  • @Fouldon GUIs already contain the logic to wait for the user to interact with any UI elements. You don't need to code this yourself. – Code-Apprentice May 17 '19 at 17:02
  • If `btnLancerLeD` is a `JButton`, then yes, an `ActionListener` is better for what you want to do. See [this example](https://stackoverflow.com/questions/42443759/set-image-to-button-and-process-actionlistener-in-puzzle-game-using-java/42449682#42449682), or [this one](https://stackoverflow.com/questions/41863777/draw-rectangle-on-pressing-jbutton/41863970#41863970) – Frakcool May 17 '19 at 17:05
  • For more help post [MCVE](https://stackoverflow.com/help/mcve) – c0der May 18 '19 at 04:57

1 Answers1

0

First, you should only call addMouseListener() once rather than doing it repeatedly in a while loop. Second, you can do the logic of the "move" all inside mouseClicked(). You probably need an if statement.

Note that Swing already contains the logic to wait for the user to do something. This is the whole point of event listeners. They are only fired when that event occurs. In the case of a button, you should use an ActionListener instead of a MouseListener. For more details, I suggest that you check out the official Oracle tutorials for Swing. You can find these with a quick Google search.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • That's what I was tempted to do but I thought that the scope of mouseClicked was preventing me from accessing all the data side of my game. – Fouldon May 17 '19 at 17:04
  • @Fouldon Scope is definitely an issue that you have to figure out here. I cannot tell from the minimal amount of code that you posted what issues will encounter nor suggest any solutions. – Code-Apprentice May 17 '19 at 17:05
  • 1
    @Fouldon It seems that you need to learn a little more about event driven programming. This is a lot different than the sequential programming you may have done with other projects. It is also separate from, but related to, MVC. – Code-Apprentice May 17 '19 at 17:07