I'm creating game that will increase it's level every 10 seconds, the users will see the time (using chronometer) and the level they currently at. I'm trying to create an integer that will be incremented every 10 seconds as shown on the chronometer.
I didn't find a guide here that is answering my needs. I tried to guess that the chronometer is running as milliseconds, which wasn't the case.
Inside my Class:
private Chronometer chronometer;
Inside my onCreate (used XML):
chronometer=findViewById(R.id.timer);
Trying to converte the chronometer to seconds (only attempted first try):
int timeChecker=(int) (SystemClock.elapsedRealtime() - chronometer.getBase());
if (timeChecker==10){
lvl++;
lvlcount.setText(lvl+"");
}
I've wanted to increase the level every 10 second and than change the TextView with the previous level.
First, the elapsedRealTime()
and chronometer.getBase()
are in millisecond units so the delta is also in milliseconds:
long timeDeltaFromStartInMs = (SystemClock.elapsedRealtime() - chronometer.getBase());
Next, assuming the chronometer is never reset for this discussion then the number of levels elapsed is (integer division truncates):
int numLevelsElapsed = (int) (timeDeltaFromStartInMs / 10000);
Assuming the initial level starts at 0 then it's simply:
lvlcount.setText(Integer.toString(numLevelsElapsed));
If the level has an initial non-zero value (from some previous play):
lvlcount.setText(Integer.toString(initialLevelCnt+numLevelsElapsed));
Might be a few other nuances to your problem - if so feel free to comment.