Tengo un pequeño juego que genera un enemigo en una posición aleatoria y sigue teletransportándose a otra posición aleatoria cada segundo. Mi problema es que no sé cómo hacer que la teletransportación sea más rápida ya que el temporizador sigue disminuyendo.
Mi temporizador comienza a los 60 segundos y disminuye hasta que llega a "0 segundos restantes" y el juego termina.
Quiero cambiar el retraso del "teletransporte" a algo como -> cada 0,8 s cuando quedan 45 segundos o cada 0,5 s cuando quedan 25 segundos, etc.
Entonces, el período del temporizador cambia automáticamente después de que transcurre una cierta cantidad de tiempo.
Esto es lo que he intentado hasta ahora, pero no hace nada. La teletransportación aún ocurre cada 1 segundo durante los 60 segundos completos.
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); }Causa principal
En Android, el temporizador programa tareas para una ejecución única o para una ejecución repetida a intervalos regulares. Eso explica por qué cuando llamas a schedule() y pasas el period , se repetirá después de ese tiempo regularmente.
Solución
Si desea programar tareas que se ejecutaron en un momento dinámico, use Handler . Así que cambia tu código a.
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; }