how do i add a number to an integer every second

2019-09-05 22:50发布

I'm making a cookie clicker sort of game and I want a thing where every second a certain number let's say 5 is added to another number. So every second the integer variable is going up by 5. How would I create a sort of time measuring method where it measures time so I can add a number to another number.

public class timeTesting {
    // I've put this method here for someone to create
    // a timer thing
    void timer()
    {

    }
    public static void main(String[] args) {        
        // Original number
        int number = 1;
        // Number I want added on to the original number every 5 seconds
        int addedNumber = 5;
    }
}

5条回答
叛逆
2楼-- · 2019-09-05 22:59

You can use Timer to schedule a TimerTask who has the desired code inside the run() method. Check the code below (run() will be called once after 5000 milliseconds) :

Timer t = new Timer();
    t.schedule(new TimerTask() {
        @Override
        public void run() {
            number += addedNumber;
        }
    }, 5000);

Also you can use scheduleAtFixedRate(TimerTask task, long delay, long period) for repetitive tasks (here run will be called immediately, and every 5000 milliseconds):

Timer t = new Timer();
    t.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            number += addedNumber;
        }
    }, 0, 5000);
查看更多
Deceive 欺骗
3楼-- · 2019-09-05 23:05

I'd like to suggest start studying RxJava. Reactive programming is very powerful for games development: https://www.youtube.com/watch?v=WKore-AkisY

With RxJava, your problem can be solved with Observable interval() method:

https://github.com/Netflix/RxJava/wiki/Creating-Observables#interval

Ivan

查看更多
成全新的幸福
4楼-- · 2019-09-05 23:16

You should consider using a Timer which will fire an event when a certain time interval has passed. It can repeat the event every so often, too.

查看更多
beautiful°
5楼-- · 2019-09-05 23:19

If you're targeting the android platform you could use CountDownTimer, which allows you to execute some code every certain amount of time for a certain duration. But be aware that android doesn't work with the main method like J2SE does.

Anyway, if you're looking foward to program an android game, i'd highly recommend you to start here: Android Development

查看更多
太酷不给撩
6楼-- · 2019-09-05 23:22

Not very elegant, but working code:

public class MyTimer {

    private volatile int number;  //must be volatile as we're working on multiple threads.
    private final int numberToAdd;
    private final long timerTimeInMillis;

    public MyTimer() {
        number = 1;
        numberToAdd = 5;
        timerTimeInMillis = 5000;
    }

    public void runTimer() {
        new Thread() {                     //declaring a new anonymous Thread class
            public void run() {            //that has to override run method.
                while (true)               //run the program in an infinite loop
                {
                    number += numberToAdd; //add a number

                    System.out.println("Added number. Now the number is: " + number);
                    try {
                        Thread.sleep(timerTimeInMillis);  //and then sleep for a given time.
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();                         //and here we're starting this declared thread
    }

    public static void main(String[] args) 
    {
        new MyTimer().runTimer();
        try {
            Thread.sleep(100000);          //this application will work for 100sec.
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Using java.util.Timer would be more elegant, but in here you may get aquainted with anonymous classes.

查看更多
登录 后发表回答