I have a small game that spawns an enemy at a random position and keeps teleporting to another random position every second. My problem is that I dont know how to make the teleportation faster as the timer keeps decreasing.
My timer starts at 60 seconds and decreases until it reaches "0 seconds left" and the game is over.
I want to change the delay of the "teleport" to something like -> every 0,8s at 45 seconds left or every 0,5s at 25 seconds left etc.
So the period of the timer changes automatically after a certain amount of time passed.
Here is what I tried so far but it does not do anything. Teleportation still happens every 1 sec throughout the whole 60 secs.
static int period;
static int periodCounter = 0;
// Load progressBar from 0s to 60s
private void progressLoad() {
final int[] counter = {pgrBar.getProgress()};
final Timer timer = new Timer();
final TimerTask timerTask = new TimerTask() {
@Override
public void run() {
counter[0]++;
periodCounter++;
pgrBar.setProgress(counter[0]);
runOnUiThread(new Runnable() {
public void run() {
tv_progress.setText("Zeit verbleibend: " + (60 - counter[0]));
}
});
if (counter[0] == 60)
timer.cancel();
if (counter[0] == 60)
gameOver();
}
};
timer.schedule(timerTask, 0, 1000);
}
// Change period variable depending on seconds left
private void changePeriod() {
if (periodCounter <= 10)
period = 1000;
else if (periodCounter <= 20)
period = 800;
else if (periodCounter <= 30)
period = 600;
else if (periodCounter <= 40)
period = 475;
else if (periodCounter <= 50)
period = 350;
}
// TimerTask for teleporting the enemy to random position (x,y) starting at every 1 sec
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Random R = new Random();
float dx = R.nextFloat() * width[0];
float dy = R.nextFloat() * height[0];
if (dx > (width[0] - imWidth[0]))
dx = width[0] - imWidth[0];
if (dy > (height[0] - imHeight[0]))
dy = height[0] - imHeight[0];
iv_muecke.animate()
.x(dx)
.y(dy)
.setDuration(0)
.start();
}
});
}
}, 0, period);
private void teleportEnemy() {
final Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
Random R = new Random();
float dx = R.nextFloat() * width[0];
float dy = R.nextFloat() * height[0];
if (dx > (width[0] - imWidth[0]))
dx = width[0] - imWidth[0];
if (dy > (height[0] - imHeight[0]))
dy = height[0] - imHeight[0];
iv_muecke.animate()
.x(dx)
.y(dy)
.setDuration(0)
.start();
}
});
}
}, 0, period);
}
Root cause
In Android, Timer schedule tasks for one-time execution, or for repeated execution at regular intervals. That explains why when you call schedule()
and pass period
, then it will repeat after that time regularly.
Solution
If you want to schedule tasks that executed at a dynamic time, then using Handler. So change your code to.
private void teleportEnemy() {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Random R = new Random();
float dx = R.nextFloat() * width[0];
float dy = R.nextFloat() * height[0];
if (dx > (width[0] - imWidth[0]))
dx = width[0] - imWidth[0];
if (dy > (height[0] - imHeight[0]))
dy = height[0] - imHeight[0];
iv_muecke.animate()
.x(dx)
.y(dy)
.setDuration(0)
.start();
if (!isGameOver()) {
handler.postDelayed(this, period);
}
}
});
}
private boolean isGameOver() {
return pgrBar.getProgress() == 60;
}