我的游戏存在问题,我无法找到问题,我的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);
}
这是Handler类,我在最后一行得到了错误 包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;
}
}
我知道很多代码,但我找不到问题,所以如果你有任何建议或想法,那么听听它们会很棒。 谢谢!
答案 0 :(得分:4)
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
}
你在方法中调用方法 - &gt; StackOverflowError
应该是:
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更改其x
和y
。
答案 1 :(得分:2)
您没有告诉我们错误是什么,但我怀疑它是堆栈溢出。这是因为您反复永久地呼叫removeObject()
。
更改
this.removeObject(object);
到
this.object.remove(object);
我还建议将LinkedList<GameObject> object
的名称更改为LinkedList<GameObject> objects
,因为它是多个对象的集合,有助于减少addObject()
和removeObject()
中的混淆参数具有相同名称的函数。
答案 2 :(得分:0)
我认为这条线是潜在的泄漏:
handler.addObject(new Trail(x, y,ID.Trail, Color.red, 16, 16, 0.02f, handler));
您正在每个刻度线创建一个Trail
类型的新对象。
这可能会导致缓慢......以及之后的崩溃。
您确定无法重复使用该对象吗?