Alin Mureșan Alin Mureșan - 3 months ago 8
Java Question

Java game, FPS drops from 300 to 1

Hello guys,
I have a problem with my game and i can't find the problem, my FPS drops from 300 to 1, every second like 5 to 10 FPS lower until it's unplayable.
it gives me an error first at wave.myFirstGame.Handler.removeObject(Handler.java:31) the games starts but crashes. but when i delete the code that gives me this error the game works but the FPS drops and still it's unplayable.


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);
}


this is the Handler class where I get the error, at the last line
package 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
}
}


and here is the Trail and the problems started when i did this.

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;
}



}


It's a lot of code i know but I can't find the problem, so if you have any suggestions or ideas it would be great to hear them.
Thank you!

Answer
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
}

You call the method inside the method -> StackOverflowError

It should be:

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
}

Also, you add a new Trail object each tick, and never remove it, so you might run out of memory. Try to add it once, in the constructor, and then change its x and y using getters and setters.

Comments