0

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

1 回答 1

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
    }
}
于 2016-12-31T12:48:01.687 回答