Pergunta

This is my code:

private Timer mTimer = new Timer();

mTimer.scheduleAtFixedRate(mTask, 0, mSpeed);

Once I called that scheduleAtFixedRate() and change my mSpeed the period doesn't change but stays the same. Which looks normal since the method clearly says "FixedRate".

But now my question: Is there something like mTimer.scheduleAtVariableRate?

I'm using this timer for my tetris, which speed increases at a certain amount of score.

I tried cancelling the mTimer and calling the same method but that doesn't work :(

Foi útil?

Solução 2

I fixed the problem by recreating the timertask as well (and creating timer again):

mTimer.cancel();
mTimer = new Timer();
mTimer.scheduleAtFixedRate(new TimerTask() { ... }, 0, mSpeed);

Outras dicas

I believe you have to avoid using "scheduleAtFixedRate", and instead simply schedule the timer to fire once, after the first period. Within your mTasks, at the end,then simply set itself to fire once more again after mSpeed.

Any changes to mSpeed should now be reflected in the changed intervals between code run. This is because the scheduler wont know of changes to mSpeed by itself, it needs to be called again with the new value.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top