0

In a tick() method, you can't have an int startTime = System.nanoTime() because it will constantly be updating it.

I need to find the elapsed time within the tick method, so that every 2 seconds a new object gets spawned.

public void tick() {

    long startTime = System.nanoTime();

        // wave 1
        if (wave == 1) {

            float k = System.nanoTime() - startTime;
            /* won't work because "startTime" is constantly updating */

            if (k >= 2 && k <= 3) {
                handler.addObject(new BasicEnemy());
            } else if (k >= 4 && k <= 5) {
                handler.addObject(new BasicObject());
            } else if (k >= 6 && k <= 7) {
                handler.addObject(new BasicEnemy());
            }
        }

        // wave 2
        if (wave == 2) {

            float k = System.nanoTime() - startTime;
            /* won't work because "startTime" is constantly updating */

            if (k >= 2 && k <= 3) {
                handler.addObject(new BasicEnemy());
            } else if (k >= 4 && k <= 5) {
                handler.addObject(new BasicObject());
            } else if (k >= 6 && k <= 7) {
                handler.addObject(new BasicEnemy());
            }
        }
    }
}

The above is a small snippet of the code. How would I go about finding the elapsed time in the tick method and having it restart its count for every if statement?

Thanks for the help :)

badProgrammer
  • 17
  • 1
  • 12
  • Are you using threads? I don't see any threads in your code... – px06 Apr 25 '17 at 17:05
  • @px06 I only have one thread in my main class, am I supposed to have more to get this to work? – badProgrammer Apr 25 '17 at 17:07
  • "/* won't work because "startTime" is constantly updating */" What makes you think that? `startTime` will always be the `nanoTime()` at the start of the method, not the _current_ `nanoTime()`. – Louis Wasserman Apr 25 '17 at 18:08

1 Answers1

0

You would need to hold the timeAtLastAccept outside the tick() method like so:

long timeAtLastAccept = System.nanoTime();
public void tick(){
    if(System.nanoTime()-timeAtLastAccept >threshold) {
         //Spawn your objects!
         timeAtLastAccept = System.nanoTime();
    } 

    //do ticky things
}
CraigR8806
  • 1,584
  • 13
  • 21
  • I did that initially, however, it continuously spawns the object every 2 seconds. – badProgrammer Apr 25 '17 at 17:16
  • @badProgrammer if you update your time variable after it spawns an object as shown above. It should wait another 2 seconds before spawning another – CraigR8806 Apr 25 '17 at 17:17
  • When you say "You would need to hold the timeAtLastAccept outside the tick() method", the only place I can initialise it would be in the constructor. And if I make the threshold = 2, _System.nanoTime()-timeAtLastAccept_ will always be larger than the threshold. – badProgrammer Apr 25 '17 at 17:22
  • @badProgrammer you have to do the calculations to convert 2 seconds to however many nanoseconds and that would be your threshold And yes I do mean to make it a property of the class and init inside of constructor – CraigR8806 Apr 25 '17 at 17:27
  • @badProgrammer Great! If it worked out for you, you can clicked the accepted checkmark next to the voting section of my post if you'd like – CraigR8806 Apr 25 '17 at 17:44