2017-08-10 9 views
0

こんにちは私のクイズゲームでタイマーに問題があります。本質的にそれは複数の選択肢のゲームであり、プレーヤーは各質問にタイムアウトします。私は、アプリケーションが起動し、プレーヤーが最初の質問を見るとタイマーが始まる。私の問題は、プレイヤーが質問に正しく答えるか、間違ってタイマーがonclickメソッドでタイマーを30秒にリセットしたにもかかわらず、ランダムな値を与え始めるということです。 30秒でタイマーを開始し、通常のカウントダウンを開始するにはどうすればよいですか?ありがとうございます。Andriod Studioのタイマーに関する問題

public class MainActivity extends AppCompatActivity { 

    //Views 
    TextView questionTextView; 
    TextView mscoreTextView; 
    TextView mtimerTextView; 
    Button mchoice1; 
    Button mchoice2; 
    Button mchoice3; 
    Button mchoice4; 


    //Constructors 
    private questions Question = new questions(); 
    private Answers cAnswers = new Answers(); 
    private choices Choices = new choices(); 

    //Variables 
    private int questionNumber = 0; 
    private int mScore = 0; 
    private String correctAnswer; 


    public void onClick(View view) { 

     Button answer1 = (Button) view; 

     if(answer1.getText() == correctAnswer) { 

      mScore = mScore + 1; 
      Toast.makeText(getApplicationContext(), "CORRECT!!", Toast.LENGTH_SHORT).show(); 
      mtimerTextView.setText("30s"); 
      runTimer(); 

     } else { 

      Toast.makeText(getApplicationContext(), "WRONG!!", Toast.LENGTH_SHORT).show(); 
      mtimerTextView.setText("30s"); 
      runTimer(); 

     } 

     updateScore(mScore); 
     updateUI(); 
    } 


    private void updateScore(int points) { 

     mscoreTextView.setText("" + points + "/" + Question.getLength()); 
    } 


    public void runTimer() { 

     new CountDownTimer(30100, 1000) { 

      @Override 
      public void onTick(long millisUntilFinished) { 

       String tick = String.valueOf(millisUntilFinished/1000 + "s"); 
       mtimerTextView.setText(tick); 
      } 

      @Override 
      public void onFinish() { 

       Toast.makeText(getApplicationContext(), "TIME RAN OUT!!", Toast.LENGTH_LONG).show(); 
       mtimerTextView.setText("0s"); 
       updateUI(); 
      } 
     }.start(); 


    } 


    private void updateUI() { 

     if (questionNumber < Question.getLength()) { 
      questionTextView.setText(Question.getQuestion(questionNumber)); 
      mchoice1.setText(Choices.getChoices(questionNumber, 1)); 
      mchoice2.setText(Choices.getChoices(questionNumber, 2)); 
      mchoice3.setText(Choices.getChoices(questionNumber, 3)); 
      mchoice4.setText(Choices.getChoices(questionNumber, 4)); 
      correctAnswer = cAnswers.getAnswer(questionNumber); 

      questionNumber ++; 

     } else { 


      Toast.makeText(getApplicationContext(), "This is the last question", Toast.LENGTH_LONG).show(); 
      //Intent intent = new Intent(MainActivity.this, HighScoreActivity.class); 
      //intent.putExtra("Score", mScore); 
      //startActivity(intent); 


     } 

     runTimer(); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     questionTextView = (TextView) findViewById(R.id.questionTextView); 
     mchoice1 = (Button) findViewById(R.id.choice1); 
     mchoice2 = (Button) findViewById(R.id.choice2); 
     mchoice3 = (Button) findViewById(R.id.choice3); 
     mchoice4 = (Button) findViewById(R.id.choice4); 


     mtimerTextView = (TextView) findViewById(R.id.timerTextView); 
     mscoreTextView = (TextView) findViewById(R.id.scoreTextView); 

     updateScore(mScore); 
     updateUI(); 
    } 
} 
+0

タイマーではなく 'TextView'を設定します... –

+0

文字列の等価性をチェックするために' == 'の代わりに' equals() 'メソッドを使用します – Yupi

答えて

1

変数をCountDownTimerクラス内に定義する必要があります。

public void runTimer() { 

     new CountDownTimer(30100, 1000) { 
     private int time = 30; 

      @Override 
      public void onTick(long millisUntilFinished) { 
       mtimerTextView.setText(time--+"s"); 
      } 

      @Override 
      public void onFinish() { 

       Toast.makeText(getApplicationContext(), "TIME RAN OUT!!", Toast.LENGTH_LONG).show(); 
       mtimerTextView.setText("0s"); 
       updateUI(); 
      } 
     }.start(); 


    } 

キャンセタイマー

あなたのタイマーが解約したい場合は、グローバル変数として定義する必要があります。

private CountDownTimer timer; // global variable 

下記のrunTimer()メソッドを呼び出してタイマーを開始します。

public void runTimer() { 

    timer = new CountDownTimer(30100, 1000) { 
     private int time = 30; 

      @Override 
      public void onTick(long millisUntilFinished) { 
       mtimerTextView.setText(time--+"s"); 
      } 

      @Override 
      public void onFinish() { 

       Toast.makeText(getApplicationContext(), "TIME RAN OUT!!", Toast.LENGTH_LONG).show(); 
       mtimerTextView.setText("0s"); 
       updateUI(); 
      } 
     }.start(); 


    } 

以下のメソッドを呼び出すことで、タイマーをキャンセルできます。これが役立つ

public void stopTimer(){ 
    if(timer != null){ 
    timer.cancel(); 
    } 
} 

希望は

1

事は、あなたが本当にあなたが発足しましたタイマーをキャンセルすることはありません、です。これに伴い、タイマーが必要なたびに新しいものを作成します。これは必須ではありません。次はあなたの問題を解決する必要があります:あなたは、クラスのフィールドにCountDownTimerを保存する必要が

private CountDownTimer timer; 

その後、アプリの起動時に一度、それを作成することができます。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
    ... 
    timer = createTimer(); 
    ... 
} 

のcreateTimer機能を:

public void createTimer() { 
    timer = new CountDownTimer(30100, 1000) { 

     @Override 
     public void onTick(long millisUntilFinished) { 
      ... 
     } 
     @Override 
     public void onFinish() { 
      ... 
     } 
} 

}

ですから、タイマーを実行する必要があるときにだけ呼び出します。

timer.start(); 

、ユーザーが答えを与えるとき、あなたが最初にタイマーを解除する必要があり、その後、再びそれを起動します。

public void onClick(View view) { 
    ... 
    timer.cancel(); 
    timer.start(); 
    ... 
} 

また:あなたが持っていますあなたのOnClick()メソッドのいくつかの重複したコード。ユーザーの答えの正確さにかかわらず、タイマーを実行してmtimerTextViewに値を設定する必要があります。したがって、基本的にはif-elseの構築外にする必要があります。

関連する問題