android - 计时器倒计时多次运行错误

时间:2017-01-04 18:33:40

标签: java android

在我的游戏中,当用户在5秒内从左向右扫描时,用户会得分。如果他从右向左扫描或需要超过5秒的时间,游戏就结束了。 我试图取消倒计时,他得到一个点刷新计时器。当用户扫错时,我也想取消它。我遇到的问题是,当用户获得一个点时,倒计时不会停止计数,当出现故障时,游戏应该结束,计时器会在停止前记下上一次运行的剩余时间并打印GameOver 。使用全局计时器是错误的吗?

  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.");     

                              }  
                    }

               }
          }
      }
    }

1 个答案:

答案 0 :(得分:1)

重新启动计时器时,我建议调用start,而不是通过调用CountDownTimer方法重新实例化load

更改

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

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