Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java how to increment an int 1 every second until it gets to 15

I'd like to know how i can add 1 to my int every second until it reaches a certain number in this case 15.

But i would only want the int to start increasing once i have pressed a button.

        @Override
        public void touchUp(InputEvent event, float x, float y,
                int pointer, int button) {
            login.addAction(Actions.moveTo(0, 310, 1));

            loginClicked = true;

            if(loginClicked == true && loginTimer == 15){
                login.addAction(Actions.moveTo(0, 430, 1));
            }
        }
    });

There is my code as you can see i am making something move then after 15 seconds if it remains untouched i want it too move back.

like image 396
James Young Avatar asked Dec 11 '22 14:12

James Young


1 Answers

You can use a Timer:

int delay = 5000; // delay for 5 sec.
int period = 1000; // repeat every sec.
int count = 0;
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask()
        {
            public void run()
            {
               // Your code

                count++;


            }
        }, delay, period);
like image 113
zennon Avatar answered Dec 21 '22 11:12

zennon