簡體   English   中英

android-隨着時間的倒數

[英]android - countdown with decreasing time

在我的游戲中,用戶在五秒鍾內單擊一個按鈕時就獲得了分數。 現在,我希望計時器減少用戶獲得的每一點的時間-例如,獲得零分的用戶有五秒鍾,而獲得一分的用戶只有4.5秒再次單擊它並獲得第二分。 我可以用for循環解決嗎?

public class GameScreen extends Activity {
    public int score = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        Button count = (Button) findViewById(R.id.button1);
        text = (TextView) this.findViewById(R.id.textView3);
        tvscore = (TextView) findViewById(R.id.score);

        timer();
    }

    public void gameover() {
        Intent intent = new Intent(this, GameOverScreen.class);
        startActivity(intent);
    }

    public void onClick (View view) {
        score++;
        tvscore.setText(String.valueOf(score));
        timer();
    }

    public void timer(){
        new CountDownTimer(5000, 10) {
            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))
                        ));
                if(animationRunning) {
                    cancel();
                }
            }
            public void onFinish() {
                text.setText("Too slow.");
                gameover();
            }
        }.start();
    }
}

怎么辦呢:

public void timer(float time) {
    new CountDownTimer(time, 10) {
        // YOUR CODE
    }
}

public void onClick (View view) {
    score++;
    tvscore.setText(String.valueOf(score));
    timer(Math.max(5000-score*500, 2000));
}

我想每次點擊(得分)將減少500毫秒的時間...

限制Math.max(a, b)將選擇最大值。 表示當5000-score*500小於2000時,它將選擇2000毫秒

唯一計時器方法:

public void timer() {
    float time = Math.max(5000-score*500, 2000)
    new CountDownTimer(time, 10) {
        // YOUR CODE
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM