2017-01-04 41 views
0

在我的遊戲中,用戶在5秒鐘內從左到右掃描屏幕時得分。如果他從右向左掃動或需要超過5秒鐘,則遊戲結束。 我想取消倒計時後,他得到一個點來刷新計時器。另外,當用戶錯誤地掃描時,我會取消它。我遇到的問題是,當用戶得到一個點時,倒計數不會停止計數,當出現故障時,所以遊戲應該結束,計時器會倒計時前一次運行的剩餘時間,然後停止並打印GameOver 。使用全局定時器是錯誤的嗎?android - 定時器倒計時錯誤的幾次運行

public class GameScreen extends Activity implements OnGestureListener { 

     private boolean animationRunning = false; 
     public int sco = 0; 
     float x1, x2; 
     float y1, y2; 
     public TextView text; 
     public TextView scorete; 
     private static final String FORMAT = "%02d:%02d"; 
     CountDownTimer mCountDownTimer; 


     @Override 
     protected void onCreate(Bundle savedInstanceState) { 

      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_game); 
      text = (TextView) this.findViewById(R.id.textView3); 
      scorete = (TextView) findViewById(R.id.textView1); 
      scorete.setText(String.valueOf(sco));   
      load(); 

     }   

     private void load() { 
      // TODO Auto-generated method stub 

      mCountDownTimer = new CountDownTimer(5000, 10) { // adjust the milli 
       // seconds here 
       public void onTick(long millisUntilFinished) { 
        text.setText("" 
          + String.format(
          "%02d:%03d", 
          TimeUnit.MILLISECONDS 
            .toSeconds(millisUntilFinished) 
            - TimeUnit.MINUTES 
            .toSeconds(TimeUnit.MILLISECONDS 
              .toMinutes(millisUntilFinished)), 
          TimeUnit.MILLISECONDS 
            .toMillis(millisUntilFinished) 
            - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS 
            .toSeconds(millisUntilFinished)))); 

       } 

       public void onFinish() { 
        text.setText("GameOver."); 

       } 
      }; 
      mCountDownTimer.start();   
     } 

     @Override 
     public boolean onTouchEvent(MotionEvent touchevent) {  

      switch (touchevent.getAction()) { 

       case MotionEvent.ACTION_DOWN: { 

        x1 = touchevent.getX(); 
        y1 = touchevent.getY(); 
        break; 
       } 
       case MotionEvent.ACTION_UP: { 

        x2 = touchevent.getX(); 
        y2 = touchevent.getY(); 

        if (!animationRunning) { 
         // if left to right sweep event on screen 
         if (x1 < x2 && (x2 - x1) >= (y1 - y2) && (x2 - x1) >= (y2 - y1)) { 

           mCountDownTimer.cancel(); 
           sco++; 
           scorete.setText(String.valueOf(sco)); 

           load(); 
         } 

           // if left to right sweep event on screen              
           else if (x1 > x2 && (x1 - x2) >= (y1 - y2) && (x1 - x2) >= (y2 - y1)) { 

           animationRunning = true;      
           mCountDownTimer.cancel();            
           text.setText("GameOver.");  

           } 
        } 

       } 
      } 
     } 
    } 
+0

確定的邏輯,如果陳述是正確的?例如,嘗試在這些語句中打印某些內容以查看它們是否已達到 –

+0

這是我的程序的摘錄,我希望我沒有將它總結爲錯誤。對我來說,這件事情運行良好,我只有定時器的問題。它永遠不會從用戶得分開始的5秒開始重新計數,但會持續下降,就像它只允許5秒一樣 –

+1

當用戶得分時移除load()會發生什麼?計時器是否停止? –

回答

1

當重新啓動計時器,我建議叫start,而不是通過調用load方法重新實例化CountDownTimer

變化

mCountDownTimer.cancel(); 
sco++; 
scorete.setText(String.valueOf(sco)); 
load(); 

mCountDownTimer.cancel(); 
sco++; 
scorete.setText(String.valueOf(sco)); 
mCountDownTimer.start();