There is a rocket that moves in a certain direction with ACTION_DOWN (when clamping) with the help of a Timer, I would like to make the user click on the screen and the rocket move, but when he lowered his finger, the rocket immediately stopped. Here is the code that was written, and in this code with ACTION_UP the timer stops, but in fact it just starts to conflict with the next pressing timer, how to fix it?
@Override public boolean onTouchEvent(final MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if(event.getY()>(Game.heightDisplay/2)) { timerTask = new MyTimerTask(); timer.scheduleAtFixedRate(timerTask, 0, 100); } else if(event.getY()<(Game.heightDisplay/2)) { timerTask2 = new MyTimerTask2(); timer2.scheduleAtFixedRate(timerTask2, 0, 100); } break; case MotionEvent.ACTION_UP: timerTask.cancel(); timerTask2.cancel(); break; } return super.onTouchEvent(event); } class MyTimerTask extends TimerTask { public void run() { if(coordY >= 930) { coordY = 930; } else { coordY = coordY + 50; } } } class MyTimerTask2 extends TimerTask { public void run() { if(coordY <= 0) { coordY = 5; } else { coordY = coordY - 50; } } }
timer.camcel();timer2.camvel();besides completing the timer task? - JuriySPb ♦