سؤال

My app is a simple game sort of like whack a mole style, it has nine lights that will flash randomly and after 20 seconds all the lights will flash. I am having a problem with the timers of this i have a timer that flashes a random light continuously and after 20 seconds i want to light up all the lights but when i do this the orignal timer keeps going and then random lights keep flashing. After all the lights flash i want it to stay with all lights on.

if(System.currentTimeMillis() < 20000)
    {
        t = new Timer();
        t.schedule(new GameLoop(),0, 1000);

        s = new Timer();
        s.schedule(new GameLoop2(), 20000);
    }
    else if(System.currentTimeMillis() > 20000){
            t.cancel();         
        }

how can i get timer t to cancel after 20 seconds?

هل كانت مفيدة؟

المحلول

This will cancel the Timer t after 20 seconds.

 Timer timer = new Timer();
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    t.cancel();
                }
            }, 20000);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top