基本上,我有一个为琐事游戏加载问题的函数。每场比赛有10轮。目前,我将它放在一个4循环中,并尝试使用CountDownTimer,但它仍然会遍历并立即调用该函数10次。只是在看一些关于如何预测持续10秒的回合然后进入下一轮的建议。
public class GameActivity extends AppCompatActivity {
int questionCount = 0;
int textUpdate = 0;
Boolean readFlag = true;
Button answerOneBtn, answerTwoBtn, answerThreeBtn, answerFourBtn;
TextView questionTextView;
int playerScore = 0;
Game currentGame = new Game(questionCount, readFlag, playerScore);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
currentGame.questionList.AnswersJumbled();
for(int i = 0; i < 10; i++) {
Log.e("Error", "For Loop is Running");
playGame();
}
answerOneBtn = findViewById(R.id.AnswerOneButton);
answerTwoBtn = findViewById(R.id.AnswerTwoButton);
answerThreeBtn = findViewById(R.id.AnswerThreeButton);
answerFourBtn = findViewById(R.id.AnswerFourButton);
questionTextView = findViewById(R.id.questionText);
answerOneBtn.setOnClickListener(v -> {
currentGame.playerOneSelection = answerOneBtn.getText().toString();
Log.e("error", currentGame.playerOneSelection);
});
answerTwoBtn.setOnClickListener(v -> {
currentGame.playerOneSelection = answerTwoBtn.getText().toString();
Log.e("error", currentGame.playerOneSelection);
});
answerThreeBtn.setOnClickListener(v -> {
currentGame.playerOneSelection = answerThreeBtn.getText().toString();
Log.e("error", currentGame.playerOneSelection);
});
answerFourBtn.setOnClickListener(v -> {
currentGame.playerOneSelection = answerFourBtn.getText().toString();
Log.e("error", currentGame.playerOneSelection);
});
}
public void playGame() {
answerOneBtn = findViewById(R.id.AnswerOneButton);
answerTwoBtn = findViewById(R.id.AnswerTwoButton);
answerThreeBtn = findViewById(R.id.AnswerThreeButton);
answerFourBtn = findViewById(R.id.AnswerFourButton);
questionTextView = findViewById(R.id.questionText);
currentGame.loadQuestion(questionCount);
questionTextView.setText(currentGame.currentQuestion);
answerOneBtn.setText(currentGame.firstAnswer);
answerTwoBtn.setText(currentGame.secondAnswer);
answerThreeBtn.setText(currentGame.thirdAnswer);
answerFourBtn.setText(currentGame.fourthAnswer);
questionCount++;
}
}发布于 2021-04-11 05:05:16
好的,经过一些挖掘,并被评论指向大致的方向,我发现正确的答案是处理器。
我的for循环现在看起来像这样
for (int i = 0; i < 10; i++) {
handler.postDelayed(() -> playGame(), 10000 * i);
playerAnswered = false;
playerScore += currentGame.checkPlayerAnswer(currentGame.playerOneSelection);
}https://stackoverflow.com/questions/67029353
复制相似问题