Java 游戏,FPS 从 300 下降到 1

Java game, FPS drops from 300 to 1

我的游戏有问题,我找不到问题,我的 FPS 从 300 下降到 1,每秒下降 5 到 10 FPS,直到无法玩为止。 它首先在 wave.myFirstGame.Handler.removeObject(Handler.java:31) 处给我一个错误,游戏开始但崩溃了。但是当我删除给我这个错误的代码时,游戏可以运行但 FPS 下降并且仍然无法玩。

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;

public class BasicEnemy extends GameObject {

private Handler handler;


public BasicEnemy(int x, int y, ID id, Handler handler) {
    super(x, y, id);

    this.handler = handler;

    velX = 5;
    velY = 5;

}

public Rectangle getBounds() { //for collision

    return new Rectangle(x, y, 16, 16);
}

public void tick() {
    x += velX;
    y += velY;
    if (y <= 0 || y >= Game.HEIGHT - 32)
        velY *= -1; // limits of the screen for the enemy

    if (x <= 0 || x >= Game.WIDTH - 16)
        velX *= -1;

    handler.addObject(new Trail(x, y,ID.Trail, Color.red, 16, 16, 0.02f, handler));
}

public void render(Graphics g) {
    g.setColor(Color.red);
    g.fillRect(x, y, 16, 16);
}

这是处理程序 class 我在最后一行收到错误的地方 包裹 wave.myFirstGame;

import java.awt.Graphics;
import java.util.LinkedList;

public class Handler {

    LinkedList<GameObject> object = new LinkedList<GameObject>();

    public void tick() {
        for (int i = 0; i < object.size(); i++) {
            GameObject tempObject = object.get(i);
            tempObject.tick();
        }
    }

    public void render(Graphics g) {
        for (int i = 0; i < object.size(); i++) {
            GameObject tempObject = object.get(i);
            tempObject.render(g);

        }

    }

    public void addObject(GameObject object) {
        this.object.add(object);
    }

    public void removeObject(GameObject object) {
        this.removeObject(object);  //this is the error and the game craches after 1 sec, if i remove it, it works but FPS drops
    }
}

这是 Trail,当我这样做时问题就开始了。

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Trail extends GameObject  {

    private float alpha = 1;

    private float life;
    private Handler handler;
    private Color color;
    private int width, height;

    //life value between  0.001 - 0.1

    public Trail(int x, int y, ID id , Color color, int width, int height, float life, Handler handler) { // constructor
        super(x, y, id);
        this.handler = handler;
        this.color = color;
        this.width = width;
        this.height = height;
        this.life = life;
    }


    public void tick() {
        if(alpha > life){
            alpha -= (life - 0.001f);
        }
        else handler.removeObject(this);

    }

    public void render(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.setComposite(makeTransparent(alpha));
        g.setColor(color);
        g.fillRect(x, y, width, height);
        g2d.setComposite(makeTransparent(1)); // we wanna sandwitch our alpha and 1

    }

    private AlphaComposite makeTransparent(float alpha){

        int type = AlphaComposite.SRC_OVER;
        return(AlphaComposite.getInstance(type, alpha));
    }

    public Rectangle getBounds() {

        return null;
    }



}

我知道很多代码,但我找不到问题所在,所以如果您有任何建议或想法,很高兴听到它们。 谢谢!

我认为这条线是一个潜在的泄漏:

handler.addObject(new Trail(x, y,ID.Trail, Color.red, 16, 16, 0.02f, handler));

您将在每次更新时创建一个 Trail 类型的新对象。 这可能会导致运行缓慢……然后导致崩溃。

您确定不能重复使用该对象吗?

public void removeObject(GameObject object) {
    this.removeObject(object);  //this is the error and the game craches after 1 sec, if i remove it, it works but FPS drops
}

你在方法里面调用方法 -> WhosebugError

应该是:

public void removeObject(GameObject object) {
    this.object.remove(object);  //this is the error and the game craches after 1 sec, if i remove it, it works but FPS drops
}

此外,您每次添加一个新的 Trail 对象,并且从不删除它,因此您可能 运行 内存不足。尝试在构造函数中添加一次,然后使用 getter 和 setter 更改其 xy

你不告诉我们错误是什么,但我怀疑是堆栈溢出。这是因为您从自身反复且永远地调用 removeObject()

改变

this.removeObject(object);

this.object.remove(object);

我还建议将 LinkedList<GameObject> object 的名称更改为 LinkedList<GameObject> objects,因为它是多个对象的集合,这将有助于减少 addObject() 和 [=12] 中的混淆=] 参数同名的函数。