-3

Hi I am new to android delveloping and I am curently making a simple game that tests your reflection when a certain color changes, to test what I have learned so far but I cant solve a problem that came up. Alright, first I will explain how the game works: When you tap the screen there is a random delay and after that you need to tap the screen again as quick as possilbe to get the best score, and if you tap earlier than when the delay is over the game stops and tells you to try again. My problem is that when I tap for the second time no matter if that is after or erlier the delay it repeats a part of a code and I cant figure out why.I posted my code that is relevant to this below.Also if you need any decleration let me know!

P.S I thing that it has somenthing to do with the handlers but i'm not sure.

    final Random random = new Random();
    final int randomNumber = random.nextInt(10) + 1;

    bestScoreView.setText("best score " + bestTime + " ms");
    mainThing.setText("Tap to start");


    mainThing.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            randomTimeDelay = randomNumber * 1000;

            if (previousTapDetected){
                mainThing.setText("You taped too fast");
                mainThing.setBackgroundColor(ContextCompat
                .getColor(getApplicationContext(), R.color.red));

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mainThing.setText("Try again");
                    }
                }, 750);


            }else if (previousTapDetected = true){
                mainThing.setText("Wait for the color to change");

                Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        previousTapDetected=false;
                        mainThing.setText("Tap now");
                        startTime = System.currentTimeMillis();
                        mainThing.setBackgroundColor(ContextCompat
                                .getColor(getApplicationContext(), R.color.red));

                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                mainThing.setText("You scored " + score + " ms");
                                mainThing.setEnabled(false);
                            }
                        }, 500);

                    }
                }, randomTimeDelay);

                endTime = System.currentTimeMillis();
                score = endTime - startTime;

                if (bestTime > score) {
                    bestScoreView.setText("Best score: " + score + " ms");
                    bestTime = score;

                } else if (bestTime  < score){
                    bestScoreView.setText("Best score " + bestTime + " ms");
                }
            }
        }

    });
Athanasios .B
  • 27
  • 1
  • 7

1 Answers1

0

As per the docs

When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

And, in your code, you are trying to manipulate the UI elements, so the Handler should be created on the UI Thread

    Handler handler = new Handler();

    mainThing.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            randomTimeDelay = randomNumber * 1000;

            if (previousTapDetected){
                mainThing.setText("You taped too fast");
                mainThing.setBackgroundColor(ContextCompat
                .getColor(getApplicationContext(), R.color.red));

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mainThing.setText("Try again");
                    }
                }, 750);
anemo
  • 1,327
  • 1
  • 14
  • 28
  • i tried to put all the handlers outside the onClickListener but again some part of the code repeats again for one more time. – Athanasios .B Nov 12 '17 at 09:24
  • There is a problem with your `if` statements. Put `Log` inside the `postDelayed()` to see which method is behaving erroneously. – anemo Nov 12 '17 at 13:52