2017-02-21 58 views
11

我有一個countDownTimer,如果用戶沒有在12日之內打gameButton第二我想叫GAMEOVER方法。問題當countDownTimer是12或者計時器只是繼續計數時,我會得到稱爲instamtly的遊戲功能。所以我試圖使用postDelayed()方法給用戶一秒鐘的時間來點擊按鈕,讓countDownTimer繼續,但是因爲我的代碼現在是遊戲在12點停止。如何在android studio中正確使用postDelayed()?

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

} 

回答

26

您幾乎使用postDelayed(Runnable, long)正確的,但只是不太。我們來看看你的Runnable。

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

當我們調用r.run();它會做的第一件事就是告訴你handler後1000毫秒運行同樣的Runnable接口,然後調用gameOver()。這實際上會導致您的方法被調用兩次:一次馬上,第二次處理器完成後,等待1000毫秒。

相反,你應該改變你的Runnable這樣的:

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

,並調用它是這樣的:

handler.postDelayed(r, 1000); 

希望這有助於。

+0

這正是我所需要的。感謝您將它簡單分解。 –

相關問題