0

When I switch screens it lags and takes a really long time to load it on android, but on desktop launcher it does it instantly.

An example of a screen that takes a long time load is the level screen, it goes there from the main menu.

This is the code in the LevelScreen it takes a really long time load this, Is there any reason for this?

public class LevelScreen extends MenuBase {


private GameMain mGameMain;


public LevelScreen(GameMain main) {
    super(main, GameInfo.LEVEL_SCREEN_NAME);

    this.mGameMain = main;

    main.ply.showAds(false);
}

@Override
protected Actor createActors() {

    Table container = new Table();
    container.setDebug(GameInfo.TABLE_DEBUG);
    container.setFillParent(true);
    container.setBackground(new TextureRegionDrawable(new TextureRegion(new Texture(AssetPaths.BLUE_BACKGROUND))));

    Table table = new Table();
    table.setBackground(new TextureRegionDrawable(new TextureRegion(new Texture(AssetPaths.BLUE_BACKGROUND))));
    table.setDebug(GameInfo.TABLE_DEBUG);


    final ScrollPane scroll = new ScrollPane(table, uiSkin);


    table.pad(GameInfo.HUD_HEIGHT).defaults().space(10);


    for (int i = 1; i < 8; i++) {

        table.row();

        final TextButton button = new TextButton("Level  " + i, uiSkin, SkinStylePath.LIGHT_BUTTON_LONG);

        table.add(button).padRight(50f);
        button.addListener(new ClickListener() {
            public void clicked(InputEvent event, float x, float y) {
                System.out.println("click " + x + ", " + y);
            }
        });

        System.out.println(GameManager.getInstance().scoreUnlockList(i));


        System.out.println("GameManager.getInstance().mGameData.getUpToWhichLevelIsUnlocked() = " + GameManager.getInstance().mGameData.getUpToWhichLevelIsUnlocked() +
                "\n GameManager.getInstance().mGameData.isLevel7Unlocked() " + GameManager.getInstance().mGameData.isLevel7Unlocked());


        if (GameManager.getInstance().mGameData.getUpToWhichLevelIsUnlocked() < i) {
            // for the levels we haven't unlocked write this next to it.
            table.add(new Label("get over " + GameManager.getInstance().scoreUnlockList(i) + " points to unlock this level", uiSkin, SkinStylePath.ARVO_WHITE_WITH_BLACK_OUTLINE_22));
            //disable all the buttons that we haven't unlocked that level for
            button.setDisabled(true);
        }


        final int finalI = i;
        button.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                clickSound(randomNumberGenerator()).play(GameManager.getInstance().mGameData.getSoundFxVolume());
                playLevel(finalI, button);
            }
        });


    }


    scroll.setFlickScroll(true);
    scroll.setFadeScrollBars(true);
    scroll.setSmoothScrolling(true);


    container.add(scroll).expand().fill();
    container.row();


    return container;
}

@Override
protected void createAnotherActor(Stage mStage) {
    Texture handTexture;
    handTexture = mAssetManager.get(AssetDescriptors.HAND);
    Image handImage = new Image(handTexture);
    handImage.setPosition(560, 100);

    SequenceAction sequenceAction = new SequenceAction(GameManager.getInstance().handMoveSequence(0.3f, false), Actions.moveTo(560f, 560f, 0.8f));
    handImage.addAction(sequenceAction);


    if (GameManager.getInstance().mGameData.getUpToWhichLevelIsUnlocked() >= 3) {
        mStage.addActor(handImage);
    }

}


void playLevel(int levelNumber, TextButton levelTextButton) {
    GameManager.getInstance().selectLevel(levelNumber);

    mGameMain.setScreen(new NutritionQuiz(mGameMain, levelTextButton.getText().toString()));
}


@Override
public void show() {

}


}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

All I can do is guess to what's slowing your game down.

The first thing I see is container.setBackground(new TextureRegionDrawable(new TextureRegion(new Texture(AssetPaths.BLUE_BACKGROUND)))); Every time you switch screens, this method is called. The thing that takes the longest in that line is probably new Texture(AssetPaths.BLUE_BACKGROUND). Doing that is much better than having static resources but it takes a long time.

One thing you could do is use a Skin. This is libgdx's way of organizing sprites, fonts, and almost anything else. It can be hard to refactor the code to make it use a skin, but in the end, it is worth it. If used correctly, instead of having multiple files with sprites, all the sprites would be compiled into a master like file so only one file would have to be loaded.

Here's an example of how I use it. As you can see, I have a skin.atlas, skin.json, skin.png. Luckily for you, you don't have to create these manually. How to use TexturePacker Is also useful as well.

If you really don't want to refactor your game into using skins, you can always store Drawables in a single object and pass that object around to each Screen so it's not loading sprites whenever the screen is changed.

retodaredevil
  • 1,261
  • 1
  • 13
  • 20
0

Please provide code of MenuBase, because it is impossible to see something from your inherited class.

Possible solutions to find performance lack:

1) Try to profile your code step by step. Create any time counter class and check each method you call after calling next screen.

2) Try to remove all extra code from your affected classes. If you have actors create method - don't call it. You have main.ply.showAds(false); - try to remove it at first, I guess this code calls something from external library. It is easily able to slow down your app.

As for @retodaredevil answer - methods like new Texture, TextureRegion, etc do not make something bad. I have interface with hundreds of such method calls and I don't see any impact on loading time by that.

AntNat
  • 44
  • 4