【问题标题】:android - countdown with decreasing timeandroid - 随着时间的减少倒计时
【发布时间】:2016-12-31 12:41:46
【问题描述】:

在我的游戏中,用户在五秒内点击一个按钮就会得到一个分数。现在我希望计时器随着用户获得的每一点而减少时间 - 例如零分用户有 5 秒的时间,当他有 1 分时,他只有 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();
    }
}

【问题讨论】:

    标签: java android android-timer


    【解决方案1】:

    这样做怎么样:

    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
        }
    }
    

    【讨论】:

    • 我可以设置一个减少的限制吗?所以它不会少于 2 秒,例如?
    • 是否也可以将timer(Math.max(5000-score*500, 2000));部分集成到定时器中而不是on click方法中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多