Java FPS计数故障

Java FPS计数故障,java,Java,我在游戏中添加了一个fps计数器,在我尝试添加它之前,它工作正常,但现在它是一个白色框,在控制台中它以惊人的速度打印0 fps: private void start() { if (running) { return; } running = true; thread = new Thread(this); thread.start(); System.out.println("Error Free!"); } public

我在游戏中添加了一个fps计数器,在我尝试添加它之前,它工作正常,但现在它是一个白色框,在控制台中它以惊人的速度打印0 fps:

private void start() {
    if (running) {
        return;
    }
    running = true;

    thread = new Thread(this);
    thread.start();
    System.out.println("Error Free!");
}

public void stop() {
    if (!running) {
        return;
    }
    running = false;
    try {
        thread.join();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(0);
    }
}

public void run() {
    int frames = 0;
    double UnproccesedSeconds = 0;
    long PreviousTime = System.nanoTime();
    double SecondsPerTick = 1 / 60.0;
    int TickCount = 0;
    boolean Ticked = false;

    while (running) {
        long CurrentTime = System.nanoTime();
        long PassedTime = CurrentTime - PreviousTime;
        PreviousTime = CurrentTime;
        UnproccesedSeconds += PassedTime / 1000000000.0;

        while (UnproccesedSeconds < SecondsPerTick) {
            tick();
            UnproccesedSeconds -= SecondsPerTick;
            Ticked = true;
            TickCount++;
            if (TickCount % 60 == 0) {
                System.out.println(frames + "fps");
                PreviousTime += 1000;
                frames = 0;
            }
        }

        if (Ticked) {
            render();
            frames++;
        }
        render();
        frames++;
    }
}

private void tick() {
}

private void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null) {
        createBufferStrategy(3);
        return;
    }
    Screen.render();

    for (int i = 0; i < WIDTH * LENGTH; i++) {
        PIXELS[i] = Screen.PIXELS[i];
    }

}
private void start(){
如果(正在运行){
返回;
}
运行=真;
线程=新线程(此);
thread.start();
System.out.println(“无错误!”);
}
公共停车场(){
如果(!正在运行){
返回;
}
运行=错误;
试一试{
thread.join();
}捕获(例外e){
e、 printStackTrace();
系统出口(0);
}
}
公开募捐{
int帧=0;
双未成功秒=0;
long PreviousTime=System.nanoTime();
双秒SPERTICK=1/60.0;
int TickCount=0;
布尔勾选=假;
(跑步时){
长CurrentTime=System.nanoTime();
long PassedTime=当前时间-上一个时间;
PreviousTime=当前时间;
未成功秒数+=PassedTime/100000000.0;
while(未成功秒

我查了一下,找不到有什么问题;如果有人能给我回电话那就太棒了

线程的优先级可能会干扰游戏输出管道。也就是说,计数器消耗了太多的运行时间,引擎管道无法继续运行,这可能就是FPS报告为0的原因


您可以尝试降低线程的优先级(当您创建线程时)和/或放置一个线程。在您的计算循环中产生一些。这确实意味着FPS计算将被分流到后面,但将允许游戏引擎正常运行(至少比原来更快)

如果有人在这里迷路了,这就是问题所在=)

这一行


在这里,所以你应该避免使用“尽快”这个词。。。我删除了它。你说fps计数器的运行速度比游戏本身快,没有给它足够的加载时间?不一定快,但它可能与渲染管道竞争,没有足够的时间让渲染器及时处理它。因此,如果我删除它,它将再次工作。但是为了这个项目,我最终不想添加它,所以我该如何更改威胁优先级?“它以前工作得很好”,所以我会想象删除它会让事情再次运行。要设置线程优先级,只需调用thread.setPriority(int),其中te降低数字,te降低优先级。你能帮我把它添加到代码中吗?我有麻烦了
while (UnproccesedSeconds < SecondsPerTick) {}
while (UnproccesedSeconds > SecondsPerTick) {}