Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
Android Studio changing TimerTask() period while executing possible?

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);
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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;
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda