Android将游戏时钟重置为原始时间

时间:2014-05-21 00:39:23

标签: android android-activity reset

我正在尝试将游戏时间重置为游戏重置时的原始时间: 我正在使用runnable来跟踪时间,如下所示:

private Runnable updateTimerThread = new Runnable() 
{
    public void run() 
    {
        if(!resetbool)
        {
            gametime = SystemClock.currentThreadTimeMillis();
            timeInMilliseconds = startTime - gametime;
        }
        else
        {
            resettime = SystemClock.currentThreadTimeMillis() - gametime;
            timeInMilliseconds = (startTime);
            reset = true;
        }
        //if(flag)
        //{
            //gamerestart = 0;
            //startTime = 60000;
            //flag = false;
        //}
        //timeInMilliseconds = startTime - gametime;
        //updatedTime = timeSwapBuff - timeInMilliseconds;
        int secs = (int) (timeInMilliseconds / 1000);
        int mins = (int) (timeInMilliseconds / 60000);
        secs = secs % 60;
        int milliseconds = (int) (timeInMilliseconds % 1000);
        if( (secs <= 0) && (mins <= 0) && (milliseconds <= 0) )
        {
            timerValue.setText("" + 0 + ":"
                    + String.format("%02d", 0) + ":"
                    + String.format("%01d", 0));
            Toast.makeText(getApplicationContext(), "Game Over",
                    Toast.LENGTH_LONG).show();
            customHandler.removeCallbacks(updateTimerThread);
        }
        else
        {
            timerValue.setText("" + mins + ":"
                    + String.format("%02d", secs) + ":"
                    + String.format("%02d", milliseconds));
            customHandler.postDelayed(this, 0);
        }
        if( (secs <= 10) && (mins <= 0) && (milliseconds <= 0) )
        {
            timerValue.setTextColor(Color.RED);
        }
    }
};

其他意味着根据游戏是否重置来跟踪时间。问题是: 1)我有一个我曾经重置的活动:

if(startButton.getText().equals("Restart"))
          {
            flag = true;
            //if(reset)
            //{
            //  gametime = resettime;
            //}
            customHandler.removeCallbacks(updateTimerThread);
            Intent i = getBaseContext().getPackageManager()
                    .getLaunchIntentForPackage(getBaseContext().getPackageName() );
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
          }

但是当执行此代码时,游戏会根据需要重置,但resetbool不会更新,并且计时器不会重置为原始时间。其他所有内容都按照我的预期重置,而OnCreate就像我预期的那样创建了所有内容。有没有办法重置systemclock或者在startActivity()执行时将计时器重置为原始值的方法?谢谢你的帮助,这已经让我感到很沮丧了。

0 个答案:

没有答案