2017-09-20 6 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(); 
     } 
    } 

したがって、私はクイズゲームを作ろうとします。答えの選択肢として4つのボタンがあります。ユーザが(any)ボタンをクリックすると、それはtimerを停止させてTurnActivity(finishTurnメソッド)を行うanswerChooseメソッドを参照します。タイマーのTextViewのように表示し、もちろんそれはTextViewにはもう変わっていない、停止(タイマ停止のように思える)CountDownTimerは実行を継続してもキャンセルを宣言します。

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

しかし、何かが後TurnActivityに達し、タイマーを停止していないようです、本当に奇妙起こりますタイマーは終了に達するまで実行を継続し、TurnActivity(もう一度)に新しいインテントを作成します。あなたが見ることができるように、私はCountDownTimerのonFinishメソッドでfinishTurnメソッドを呼び出しました。したがって、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

実行フローを確認するためにブレークポイントを使用してデバッグすることはできますか。 'cancel()'はカウントダウンをキャンセルします。 – Raghunandan

+0

@RaghunandanそれはmCancelled = trueだと言っています –

+0

メソッドを宣言しても呼び出されません。あなたが何を求めているのか不明です。 – EJP

答えて

0

問題を解決しました。私がしなければならないのは、onStopメソッドをオーバーライドすることだけです。

@Override 
protected void onStop() { 
    super.onStop(); 
    gameTime.cancel(); 
} 
関連する問題