How to put a wait time for the code to run?

Asked

Viewed 3,102 times

2

I’m playing a little game of memory, more like learning in Java. I will present the code and then explain the question.

 private void testar (ImageView img, int resultado1, int resultado2) {

    if(resultado1 != resultado2 && resultado2 != 12){
        teste.setText("diferente");
        //quero colocar um temporizador aqui!!!
        img.setImageResource(R.drawable.cartatras);

        if (flag == 1){
            carta1.setImageResource(R.drawable.cartatras);
        } else if (flag == 2) {
            carta2.setImageResource(R.drawable.cartatras);
        } else if (flag == 3) {
            carta3.setImageResource(R.drawable.cartatras);

Basically it’s a function that I did that compares the first and second card chosen by the user, and if it’s different, it arrow the flipped card in the two chosen by the user.

The problem is this, I call this method in OnClick from the moment the second card is chosen, but there is not even time to see which was the second card, he already arrow her as cartatras and the user has no time to see which card it was.

So I need some way to make the code wait a little bit, so the user first sees what the card was, and then sets it face down. I tried to use the Thread.sleep in a try/catch, but it didn’t work.

2 answers

1

0

Browser other questions tagged

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