简体   繁体   中英

Slow down rendering graphics in java

When I run the code below the beginning position of x and y and the final position of x and y render. I want each position of x and y to render as the tick method runs and changes the x and y values by the speed. Basically I want to be able to control the speed of an object as it moves.

public void render(Graphics g) {
           g.drawImage(JamesTexture.james, x, y, width, height, null);
    }

    public void tick() {

        if (run) {
            if (!stop) {

                    int z = Maze.fx.size() - 1;
                    int i = 1;// speed
                    xx = getPathX(z);
                    yy = getPathY(z);

                    while (z > 0) {

                        if (xx > (getPathX(z - 1))) {
                            xx = xx - i;
                            x = xx;
                            if (xx < (getPathX(z - 1))) {
                                xx = (getPathX(z - 1));
                            }
                        } else if (xx < (getPathX(z - 1))) {
                            xx = xx + i;
                            x = xx;
                            if (xx > (getPathX(z - 1))) {
                                xx = (getPathX(z - 1));
                            }
                        } else if (yy > (getPathY(z - 1))) {
                            yy = yy - i;
                            y = yy;
                            if (yy < (getPathY(z - 1))) {
                                yy = (getPathY(z - 1));
                            }
                        } else if (yy < (getPathY(z - 1))) {
                            yy = yy + i;
                            y = yy;
                            if (yy < (getPathY(z - 1))) {
                                yy = (getPathY(z - 1));
                            }
                        } else {

                            z--;
                            stop = true;                        }
                    }
                }
            }
        }
    }

    int getPathX(int z) {
        List<Integer> fx = Maze.fx;
        z = fx.get(z);
        return z * 32;
    }

    int getPathY(int z) {
        List<Integer> fy = Maze.fy;
        z = fy.get(z);
        return z * 32;
    }

Just a suggestion as I cannot comment yet, but it might be that your while loop finishes the entire maze in one call. I think we'd need more of your code to be sure, but try to add a break statement at the end of each if statement in your while loop like so:

if (xx > (getPathX(z - 1))) {
    xx = xx - i;
    x = xx;
    if (xx < (getPathX(z - 1))) {
        xx = (getPathX(z - 1));
    }

    break;
}

Check if that fixes it. It is of course dependant on the speed the tick() function is called with.

我知道它可能已经被杀死了,但是Thread类有一个.sleep方法,该方法以纳秒为参数。

There is no perfect way to handle this, because you can never exactly 'predict' how much time will be spent until the image that you are rendering is actually displayed. What you can do is measure the time(s) that elapsed between the previous tick(s) and the current tick.

delta time = current tick time - last tick time

This gives you the actual delta time that elapsed. So how do you fetch the tick time? I believe in Java the Clock, System and Timer classes provide various functions that each come with different precision and granularity. Typically, some of these functions depend on the hardware clock, some might be interrupted by various software and hardware events, some experience problems when multiple hardware cores are context switching, some might occasionally even run backwards.. It's a zoo and you need to read the docs carefully to understand what you are actually measuring. Generally though, millisecond precision is decent enough for most applications. Eg this gets you the time in seconds (as a double with plenty of precision for millisecond ranges).

double stime = System.currentTimeMillis();

The speed is generally expressed as distance per time unit such as

meters per second (m / s)

You can then compute the distance the object needs to move by multiplying the delta time that elapsed with the speed.

delta object movement = delta elapsed time * speed

If you want to slow down the movement of your objects, you reduce the speed.

If you have a very 'stable' system framerate (and if you use standard double buffering this is mostly the case), you might be ok with a delta between the last 2 ticks, but of course you can use different prediction schemes to try to anticipate slowdowns if you have huge bursts of processing at irregular intervals. Getting a predictable framerate is why making good games is still hard to do.

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