2017-09-20 5 views
-2
public class GameActivity extends AppCompatActivity { 
    TextView txtTurn, txtScore, txtQuestion, txtQuestionNumber, txtTimer; 
    Button[] btnAnswers; 
    Player players[]; 
    ArrayList<Question> questions; 
    Question currentQuestion; 
    CountDownTimer gameTime; 
    Boolean answered, timesup; 

    int score, turn, round; 
    final int TIMES = 11000; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_game); 

     getSupportActionBar().hide(); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     txtTurn = (TextView)findViewById(R.id.txtTurnGame); 
     txtScore = (TextView)findViewById(R.id.txtScoreGame); 
     txtTimer = (TextView)findViewById(R.id.txtTimer); 
     txtQuestionNumber = (TextView)findViewById(R.id.questionNumber); 
     txtQuestion = (TextView)findViewById(R.id.txtQuestion); 
     btnAnswers = new Button[4]; 
     btnAnswers[0] = (Button)findViewById(R.id.answer1); 
     btnAnswers[1] = (Button)findViewById(R.id.answer2); 
     btnAnswers[2] = (Button)findViewById(R.id.answer3); 
     btnAnswers[3] = (Button)findViewById(R.id.answer4); 
     answered = false; 
     timesup = false; 

     Intent intent = getIntent(); 
     players = MainActivity.players; 

     turn = intent.getExtras().getInt("turn"); 
     score = players[turn].getScore(); 
     round = intent.getExtras().getInt("round"); 
     questions = MainActivity.shuffledQuestionList; 
     currentQuestion = questions.get(round/2); 

     Integer[] randomAnswer = new Integer[4]; 
     for (int i = 0; i < randomAnswer.length; i++) { 
      randomAnswer[i] = i; 
     } 
     Collections.shuffle(Arrays.asList(randomAnswer)); 

     txtTurn.setText(TurnActivity.txtTurn1.getText()); 
     txtScore.setText("Score : " + Integer.toString(score)); 
     txtQuestion.setText(currentQuestion.getQuestion()); 
     txtQuestionNumber.setText("QUESTION #" + Integer.toString(round/2+1)); 
     for (int i = 0; i < randomAnswer.length; i++){ 
      String[] answers = currentQuestion.getAnswersList(); 
      btnAnswers[i].setText(answers[randomAnswer[i]]); 
     } 

     gameTime = new CountDownTimer(TIMES, 1000){ 
      public void onTick(long millisUntilFinished) { 
       if(!timesup) 
        txtTimer.setText("Time : " + millisUntilFinished/1000 + "s"); 
      } 

      public void onFinish() { 
       txtTimer.setText("Time's Up"); 
       timesup = true; 
       finishTurn(); 
      } 
     }; 
     gameTime.start(); 
    } 

    public void answerChoose(View v){ 
     if(!answered && !timesup){ 
      answered = true; 
      gameTime.cancel(); 
      Button btnPressed = ((Button) v); 
      if(btnPressed.getText().equals(currentQuestion.getAnswer())){ 
       btnPressed.setBackgroundColor(Color.GREEN); 
       players[turn].setScore(score+1); 
      } 
      else{ 
       btnPressed.setBackgroundColor(Color.RED); 
      } 

      finishTurn(); 
     } 
    } 

    private void finishTurn(){ 
     gameTime.cancel(); 
     if(round+1 >= 10){ 
      final Intent intent = new Intent(getApplicationContext(), ResultActivity.class); 
      startActivity(intent); 
      finish(); 
     } 
     else { 
      final Intent intent = new Intent(getApplicationContext(), TurnActivity.class); 
      new CountDownTimer(2000, 1000) { 
       public void onTick(long millisUntilFinished) { 

       } 

       public void onFinish() { 
        //Toast.makeText(getApplicationContext(), "Player2 Time!", Toast.LENGTH_SHORT).show(); 
        intent.putExtra("turn", turn); 
        intent.putExtra("round", round + 1); 
        startActivity(intent); 
        finish(); 
       } 
      }.start(); 
     } 
    } 

Alors, j'essaie de faire un jeu-questionnaire. Il y a 4 boutons comme choix de réponse. Lorsque l'utilisateur clique sur un bouton (quelconque), il se réfère à la méthode answerChoose qui arrête le temporisateur et fait une intention à TurnActivity (méthode finishTurn). La minuterie apparaît comme TextView et bien sûr, il est arrêté, le TextView ne change plus (comme semble le chronomètre arrêté)CountDownTimer continue de s'exécuter même déclaré cancel()

public void answerChoose(View v){ 
    if(!answered && !timesup){ 
     answered = true; 
     gameTime.cancel(); 
     Button btnPressed = ((Button) v); 
     //Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); 
     if(btnPressed.getText().equals(currentQuestion.getAnswer())){ 
      btnPressed.setBackgroundColor(Color.GREEN); 
      players[turn].setScore(score+1); 
     } 
     else{ 
      btnPressed.setBackgroundColor(Color.RED); 
     } 

     finishTurn(); 
    } 
} 

Mais quelque chose arrive vraiment bizarre, après atteint le TurnActivity, la minuterie ne semble pas arrêter , la minuterie continue à fonctionner jusqu'à atteindre la fin et faire une nouvelle intention à TurnActivity (encore une fois). Comme vous pouvez le voir, j'ai appelé la méthode finishTurn dans la méthode onFinish de CountDownTimer. Ainsi, la minuterie elle-même n'a pas arrêté même la méthode d'annulation déclarée dans la méthode answerChoose.

gameTime = new CountDownTimer(TIMES, 1000){ 
     public void onTick(long millisUntilFinished) { 
      if(!timesup) 
       txtTimer.setText("Time : " + millisUntilFinished/1000 + "s"); 
     } 

     public void onFinish() { 
      txtTimer.setText("Time's Up"); 
      timesup = true; 
      finishTurn(); 
     } 
    }; 
+0

pouvez-vous déboguer avec des points de rupture pour vérifier le flux d'exécution. 'cancel()' annule le compte à rebours. – Raghunandan

+0

@Raghunandan dit mCancelled = true –

+0

Les méthodes de déclaration ne les appelle pas. Pas clair ce que vous demandez. – EJP

Répondre

0

Problème résolu. Tout ce que j'ai à faire est de remplacer la méthode onStop.

@Override 
protected void onStop() { 
    super.onStop(); 
    gameTime.cancel(); 
}