0

I am still a very early coder and still don't know everything about java so my code is still a bit messy, sorry about that. I am making a simple platformer game with the graphics g class and I'm trying to figure out how to delay a method without pausing the entire script.

I have tried Thread.sleep() and TimeUnit.SECONDS.sleep() but both of these freeze other methods running at the time, and the Timer that keeps the game running.


    private static Timer timer;
    private int delay = 10;

    public Gameplay() {
        addKeyListener(this);
        setFocusable(true);
        setFocusTraversalKeysEnabled(false);
        timer = new Timer(delay, this);
        timer.start();
    }

    public void moveDown() {
        if (play == true) {
            Playsound("drop");
            dropping = true;
            //pause here for 1 second without freezing timer or other running methods
            dropping = false;
        }
    }

I want the program to continue running while waiting, but the things I have tried always freeze the entire program

  • Doing a "simple platformer" is quite a task for somebody new to programming. What you are trying to do is to separate the event loop (which takes care of updating the game state) from everything else. This is called multi threading and is kinda next level :-). You could start with chunking your game loop into 50 pieces of 20 ms. Take *one* input (or no input), calculate the next game state (what is where etc.), draw the new screen and sleep for the remainder of the 20 ms. This will give you roughly 50 FPS and does not need more than one thread. Hope this helps a bit. – Christoph Grimmer Aug 05 '19 at 17:32
  • Hmm, depending how you go about this I think you can get away with using something like the [Timer](https://docs.oracle.com/javase/7/docs/api/java/util/Timer.html) to execute a given task after a set amount of time like described [here](https://stackoverflow.com/questions/2258066/java-run-a-function-after-a-specific-number-of-seconds). Since your delay is to just wait a bit before setting a boolean it should be fairly straight forward. – Tim Hunter Aug 05 '19 at 18:01
  • Here's another article that better explains the usage of util Timer and TimerTask as well as ExecutorService: https://www.baeldung.com/java-timer-and-timertask – Tim Hunter Aug 05 '19 at 18:11

2 Answers2

0

One trick from my side

public void moveDown() {
    if (play == true) {
        Playsound("drop");
        dropping = true;
        //create a thread which will execute your method and set sleep on that thread

        dropping = false;
    }
Suman Dey
  • 66
  • 1
  • 10
0

try this

...
    Thread thread = new Thread(() -> moveDown());
    thread.run();

...

    void moveDown() {
        //do some work
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //continue your work
    }

explanation

you need to stop thread , but you dont want to stop current thread :)

that means you need to run your work on background thred and stop only it,

this is not best solution , its just for explanation how you can do it.

Hayk Melkonyan
  • 2,110
  • 4
  • 16
  • 22