Prevent a second click on the same button

Asked

Viewed 1,194 times

0

In this memory game, when I click the first button and then the second button, everything happens according to the algorithm, but when I click two clicks on the first button, there is a problem... I wanted to know how to avoid a second click on the first button clicked, type disable it so it can not receive a second click... because the button counts every click it receives, so there can be no more than one click.

            for (int i=0; i<16; ++i){

            if (event.getSource() == buttons[i]){
                buttons[i].setGraphic(new ImageView(imgs[Aleatorio[i]]));//novo código
                //buttons[i].setVisible(true);
                NumeroClick++;
                if (NumeroClick == 1) PrimeiroClick = i;
                if (NumeroClick == 2){
                    SegundoClick = i;

                    ///////////////Clicks_não_conseguidos///////////////
                    if (Aleatorio[PrimeiroClick] != Aleatorio[SegundoClick]){     

                        pontos-=2;

                    Servico service = new Servico();

                    service.setOnSucceeded((WorkerStateEvent ev) -> {

                        buttons[PrimeiroClick].setGraphic(null); //novo código
                        buttons[PrimeiroClick].setDisable(false);                            
                        buttons[SegundoClick].setGraphic(null); //novo código
                        buttons[SegundoClick].setDisable(false); 

                    });               

                        service.start();

                    }  else {
                        ContAcertos++;
                        pontos+=10;
                    }
                    NumeroClick = 0;
                }
            }
        }
  • I think you’re missing one if if I understood your code well... After knowing that it is the second click , here I think if (NumeroClick == 2){ you have to check that it’s not on the same button as the first one, if it’s the same, you have to repeat the click... However I didn’t understand how your code is executed, since you always walk inside the FOR ... In my logic I should wait for a click and then check if it was the first or the second if it is the second and with the first stored in a variable you make the comparison whether it is right or not...

  • Are you using jQuery ?

1 answer

2

A viable solution would be to create a Listener(Listener) for when your Muffin is clicked or undergo any change, he alter his status even when you want.

Another would be for you to add one Handler to the button.


Example 1:

Button x = new Button();
    x.onActionProperty().addListener(listenerEvent -> {
        x.setDisable(true);
    });

//...
// Até certo momento

x.setDisable(false);

Example 2:

Button x = new Button();
x.addEventHandler(ActionEvent.ACTION, event -> {
    x.setDisable(true);
});

//...
// Até certo momento

x.setDisable(false);

Take a look at this one page in English that speaks exactly of it!

  • I did something similar, Yuri, but the button looks kind of transparent, different from the original.

  • 1

    In this case, it is expected that he will stay like this, is the deactivated state, avoiding clicks, the moment you do x.setDisable(false) he goes back to normal state. Thus it avoids operating without necessity, in the case here of two clicks. setDisabled

  • You can create logic within the Listener or Handler starting with Disable true and ending with Disable false. A suggestion in the case.

  • In my algorithm, it is difficult to use Disable; because the moment I vote to enable, the problem will return if the button is clicked again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.