Jon
Jon

Reputation: 319

libGDX game acting slow

I have a game, and when I get to the GameScreen, it gets extremely slow fairly quickly. On the GameScreen, if I just do nothing and watch, and the FPS drops to under 10 and stays there in around 30 seconds. I've been looking around my code, and I think I've narrowed it down to a section, but it makes no sense to me why it's not working, or how to fix it.

start_button = new TextButton("Start", Resources.getSkin());
    start_button.setWidth(75);
    start_button.setHeight(25);
    start_button.setX(FRUSTUM_WIDTH / 10); 
    start_button.setY(FRUSTUM_HEIGHT / 4);

    ...

    stage.addActor(start_button);
    stage.addActor(pause_button);
    stage.addActor(reset_button);
    stage.addActor(platform_button);

In my GameScreen's render method, I call a method, and in that method I created four textbuttons. The section of code that seems to be the problem is the last four lines when I add the buttons to the stage. If I comment out those lines, the game works fine and the FPS is constantly 60. If I only comment out three of the lines, and add one button to the stage, the FPS will still drop to under 10, but it takes longer and eventually spikes back up to 60.

Why are those lines slowing down the game?

Upvotes: 0

Views: 948

Answers (1)

Paras Mittal
Paras Mittal

Reputation: 1149

If you are doing all this in your Gamescreen's render method then this is your mistake.

because every time you are creating new button and adding it to the stage which is definately not good. Every time you add something to stage its list increases and if depending on how frequent your render method is called the list increases and so is the time to process that list.

Dont forget about the garbage collector because lots of objects are being made and when garbage collector will be called your fps will definately decrease.

Upvotes: 4

Related Questions