Search code examples
javaandroidtimerlibgdxcountdown

in libgdx how do you print a count down timer?


i want my game to run for ten seconds print the seconds and when it hits 0 the game is over. As of right now i can print from 0 to 10 and it makes it game over at 10 seconds but i need the reverse.

   time+= delta;
    System.out.println(String.format("time: %,.2f", time));

    if(!tOn) {
             tOn = true;

             Timer.schedule(new Task() {

                @Override
                public void run() {
                     currentState = GameState.GAMEOVER;

                }

             }, 10);
        }

Solution

  • Set your variable time = 10 and decrement it as time-- inside your Timer and print the same.

        int time = 10;
    
        System.out.println(String.format("time: %,.2f", time));
    
        if(!tOn) {
                 tOn = true;
                 Timer.schedule(new Task() {
    
                    @Override
                    public void run() {
                         time--;
                         if(time == 0){
                           currentState = GameState.GAMEOVER;
                         }
                    }
                 }, 10);
            }