2017-02-21 19 views
11

Tôi có một countDownTimer và nếu người dùng không nhấn nút gameButton trong vòng thứ hai, tôi muốn phương thức gameOver được gọi. vấn đề tôi hoặc nhận được trò chơi chức năng gọi là instamtly khi countDownTimer là 12 hoặc bộ đếm thời gian chỉ giữ đếm ngược. Vì vậy, tôi đang cố gắng sử dụng phương thức postDelayed() để cung cấp cho người dùng một giây đầy đủ để nhấn nút và để countDownTimer tiếp tục nhưng vì mã của tôi là ngay bây giờ trò chơi dừng lại trên 12 bất kể.cách sử dụng postDelayed() một cách chính xác trong studio android?

import android.app.Activity; 
import android.os.CountDownTimer; 
import android.os.Handler; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 



public class GameScreen extends Activity { 

    private TextView time; 
    private Button start; 
    private Button cancel; 
    private Button gameButton; 
    private CountDownTimer countDownTimer; 
    public static int count = 0; 
    public static int countFail = 0; 

    final Handler handler = new Handler(); 
    final Runnable r = new Runnable() { 
     public void run() { 
      handler.postDelayed(this, 1000); 
      gameOver(); 
     } 
    }; 


    private View.OnClickListener btnClickListener = new View.OnClickListener(){ 

     @Override 
     public void onClick(View v) { 

      switch(v.getId()){ 
       case R.id.start_ID : 
        start(); 
        break; 
       case R.id.cancel : 
        cancel(); 
        break; 
       case R.id.gameButton_ID : 
        gameButton(); 
        break; 
      } 

     } 


    }; 


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


     start = (Button) findViewById(R.id.start_ID); 
     start.setOnClickListener(btnClickListener); 
     cancel = (Button) findViewById(R.id.cancel); 
     cancel.setOnClickListener(btnClickListener); 
     time = (TextView) findViewById(R.id.time); 
     gameButton = (Button) findViewById(R.id.gameButton_ID); 
     gameButton.setOnClickListener(btnClickListener); 


    } 

    public void start(){ 

     time.setText("16"); 
     //this doesnt work and makes app crash when you hit start button 

     countDownTimer = new CountDownTimer(16 * 1000, 1000) { 
      @Override 
      public void onTick(long millsUntilFinished){ 
       time.setText("" + millsUntilFinished/1000); 

       //turns textview string to int 
       int foo = Integer.parseInt(time.getText().toString()); 

       if(time.getText().equals("12")){ 

        r.run(); 

       } 

      } 

      public void onFinish() { 
       time.setText("Done !"); 
      } 
     }; 
     countDownTimer.start(); 
    } 

    private void cancel(){ 
     if(countDownTimer != null){ 
      countDownTimer.cancel(); 
      countDownTimer = null; 
     } 
    } 

    private void gameOver(){ 
     Toast.makeText(getApplicationContext(), "You scored " + count, Toast.LENGTH_SHORT).show(); 
     count = 0; 
     countFail = 0; 
     cancel(); 
    } 

    private void gameButton(){ 

     int foo = Integer.parseInt(time.getText().toString()); 

     if(foo % 2 == 0) { 
      Toast.makeText(getApplicationContext(), "PASS", Toast.LENGTH_SHORT).show(); 
      handler.removeCallbacks(r); 
      ++count; 
     } 

     else{ 
      gameOver(); 
     } 
    } 

} 

Trả lời

26

Bạn gần như đang sử dụng postDelayed(Runnable, long) chính xác, nhưng không hoàn toàn. Chúng ta hãy nhìn vào Runnable của bạn.

final Runnable r = new Runnable() { 
    public void run() { 
     handler.postDelayed(this, 1000); 
     gameOver(); 
    } 
}; 

Khi chúng ta gọi là r.run(); điều đầu tiên nó sẽ làm là nói với handler của bạn chạy rất giống Runnable sau 1000 mili giây, và sau đó gọi gameOver(). Điều này thực sự sẽ dẫn đến phương thức gameOver() của bạn được gọi hai lần: một lần ngay lập tức và lần thứ hai sau khi Trình xử lý được thực hiện chờ 1000 mili giây.

Thay vào đó, bạn nên thay đổi Runnable của bạn như thế này:

final Runnable r = new Runnable() { 
    public void run() { 
     gameOver(); 
    } 
}; 

Và gọi nó là như thế này:

handler.postDelayed(r, 1000); 

Hope this helps.

+0

Chỉ cần những gì tôi cần. Cảm ơn bạn đã phá vỡ nó một cách đơn giản. –

Các vấn đề liên quan