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");
}
}
}
});