16

I'm new to Java and making a small game for practice.

if (doAllFaceUpCardsMatch == false) {
        //run pause here//
        concentration.flipAllCardsFaceDown();
} else {
        concentration.makeAllFaceUpCardsInvisible();
}

I want to pause the game for two seconds here before it does

concentration.flipAllCardsFaceDown();

How would I go about pausing it?

Ethanph89
  • 187
  • 1
  • 1
  • 8
  • 5
    Possible duplicate of [How to delay in Java?](http://stackoverflow.com/questions/24104313/how-to-delay-in-java) – Manav Dubey Apr 19 '17 at 23:14
  • 4
    `Thread.sleep` would be the obvious choice, however, if you're using a GUI like Swing or JavaFX, you shouldn't do this from within the context of their dispatching threads, that would be really, really bad. Most UI frameworks will have their own defined mechanisms for doing this, but we don't have that information – MadProgrammer Apr 19 '17 at 23:15
  • When I do Thread.sleep() I get: "error: unreported exception InterruptedException; must be caught or declared to be thrown Thread.sleep(2000);" when compiling – Ethanph89 Apr 19 '17 at 23:18
  • 1
    @Ethanph89 well, the compiler is telling you exactly what you have to do: catch the `InterruptedException`, or declare it to be thrown. – Andy Turner Apr 19 '17 at 23:23
  • Incidentally, don't explicitly compare to `true` and `false`. – EJoshuaS - Stand with Ukraine Apr 20 '17 at 01:44

5 Answers5

27

You can use:

 Thread.sleep(2000);

or

java.util.concurrent.TimeUnit.SECONDS.sleep(2);

Please note that both of these methods throw InterruptedException, which is a checked Exception, So you will have to catch that or declare in the method.

Edit: After Catching the exception, your code will look like this:

if (doAllFaceUpCardsMatch == false) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        concentration.flipAllCardsFaceDown();
} else {
        concentration.makeAllFaceUpCardsInvisible();
}

Since you are new, I would recommend learning how to do exception handling once you are little bit comfortable with java.

Ashish Gupta
  • 567
  • 4
  • 7
  • Thanks for the help! Like I said, I'm new to Java (and most programming in general)- how would I catch the Exception? – Ethanph89 Apr 19 '17 at 23:22
2

For those just wanting a quick hack without having to bring in a library...

public class Timing {
    public static void main(String[] args) {
            int delay = 1000; // number of milliseconds to sleep

            long start = System.currentTimeMillis();
            while(start >= System.currentTimeMillis() - delay); // do nothing

            System.out.println("Time Slept: " + Long.toString(System.currentTimeMillis() - start));
    }
}

For high precision 60fps gaming this probably isn't what you want, but perhaps some could find it useful.

Scotty Stephens
  • 359
  • 3
  • 7
1

You can find a similar post here: How to delay in Java?

Basically what says in the old post. You could use java.util.concurrent.TimeUnit

    import java.util.concurrent.TimeUnit;

    if (doAllFaceUpCardsMatch == false) {
        TimeUnit.SECONDS.sleep(2);
        concentration.flipAllCardsFaceDown();
    } else {
        concentration.makeAllFaceUpCardsInvisible();
    }
Community
  • 1
  • 1
0

You can use Thread.currentThread().sleep(2000) to pause the current thread for 2 seconds (2000 milleseconds). You should surround this with a try/catch in case of InterruptedExceptions.

ILIkeEggs
  • 1
  • 1
0

Another very simple way is creating the following method:

public static void pause(long timeInMilliSeconds) {

    long timestamp = System.currentTimeMillis();


    do {

    } while (System.currentTimeMillis() < timestamp + timeInMilliSeconds);

}
ACSJR
  • 1
  • While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Brian61354270 Apr 10 '20 at 01:56
  • I guess that's ok for anyone who knows what a method is, and how to invoke it. If one doesn't know, this one shouldn't be here copying and pasting code, but doing some basic Java course. There's a very straight line between giving a helping hand and chainning a soul. – ACSJR Apr 12 '20 at 01:55