简体   繁体   中英

How do I delay one image from showing on the screen in Java?

I'm so far working on a Pacman game for my grade 12 computer science.

I want to allow the monsters to automatically move around the game. I'm using Slick2D and LWJGL for my game. I have this bit of code in my game that controls where the blue monster is moving.

if (bluePosX < 400) {
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    bluePosX++;
}

So basically what I see on the screen is the blue monster automatically moving towards the right direction. My only problem is when I use Thread.sleep(100); it delays the entire screen. I just want to delay how long the blue monster should wait before moving in the right direction. How do I only delay the blue monster image and not the entire screen?

I assume that you set how often the game loop updates, eg 60 times a second = 60fps. In that case 100ms is equal to 6 updates (each update being spaced 1000/60 ms apart). So just wait 6 updates before changing bluePosX++ .

I recommend you to work with the delta you have in your update method.

See: this

You can count the delta up in a variable and execute a certain action after the maximum delta is reached. After that you can reset the deltaCounter to 0. I wouldn't recommend using Thread.sleep or anything similiar. You could use Java Swing Timer but usually timing related things are achieved by using the delta. It's easier and provided by the engine you use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM