Search code examples
javaperformanceframe-rate

Low FPS in Java


Hello my name is Ryan and I'm currently developing my own 2D java game. Currently there are a lot of objects within the game world. Upon a new start of the game, the world loads with 100 tress randomly positioned on it, made with the use of an arraylist and a tree class. My game uses a class called checkcollisions to check if the player is intersecting with any tress. This method is then put within the update method. When this method is not called I get an extra 100 FPS is there away I can still get this 100 fps but still check for collisions? I really need an FPS boost asas my game currently runs at 30-50 fps

here is the checkcollisions code:

public void checkCollisions() {
    for (int i = 0; i < Placing_Objects.Small_Trees.size(); i++) {
        if (player.getBounds().intersects(Placing_Objects.getSmall_Tree().get(i).getBounds())) {
            if (gotAxeOn) {Placing_Objects.Small_Trees.get(i).health -= rand.nextInt(3);}
        }
        if (Placing_Objects.Small_Trees.get(i).health <= 0) {
            Placing_Objects.removeSmall_Tree(Placing_Objects.Small_Trees.get(i));
            Inventory.addItemToInv("Wood");
            Inventory.addItemToInv("Wood");
            Inventory.addItemToInv("Stick");
            Player.exp += rand.nextInt(3);
            challenges.choppedDownTrees += 1;
        }
    }
}

Solution

  • I just rearranged the code and it now works fine! Thank you to everyone who helped, i'm also sorry for posting this question many times...I'm new to this and didn't really know what I was doing. Once again thank you everyone you've helped a developer in need! Here's the new code:

    for (int i = 0; i < Placing_Objects.Small_Trees.size(); i++) {
            if (gotAxeOn) {
                if (player.getBounds().intersects(Placing_Objects.getSmall_Tree().get(i).getBounds())) {
                    Placing_Objects.Small_Trees.get(i).health -= rand.nextInt(3);
                    }
            }
            if (Placing_Objects.Small_Trees.get(i).health <= 0) {
                Placing_Objects.removeSmall_Tree(Placing_Objects.Small_Trees.get(i));
                Inventory.addItemToInv("Wood");
                Inventory.addItemToInv("Wood");
                Inventory.addItemToInv("Stick");
                Player.exp += rand.nextInt(3);
                challenges.choppedDownTrees += 1;
            }
        }