Essa Rebaño
Essa Rebaño

Reputation: 103

Libgdx: screen managaer

Good day sirs, I've started to develop a game which uses a lot of screens(introscreen,splashscreen,previewscreen,mainmenuscreen, etc.) and have encountered some of the following problem:

switching from screen to other screen are sometimes unresponsive or buggy, and sometimes repeats the same screen over and over again. I have recycled a back button which i planned on using in different windows, but it is only visible in one of the windows but in the other. I implements my screens to SCREENS, does this have to do anything with abstract screen class because as i have seen in different sample codes they extend their screen to a certain class which implements a screen.

I'm quite confuse with this part, I hope could find a proper solution to this problem.

Is there a way which i could properly navigate or make it more responsive my screens?would it help improve the performance of the game i would create an screenmanager?what instances should a screenmanager have?

this is my code for my mainmenuscreen which encounters a problem with the back button.

public class MainMenuScreen implements Screen {

public static final String LOG = "MainMenu Class";
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture = Assets.bgmenu;
private Sprite sprite;
private static Stage stage;
private static DugManMainClass game;
private TextButton tb;
private TextButton tb2;
private TextButton tb3;
private TextButton tb4;
private TextButton tb5;
private TextButton load;
private TextButton newgame;
private TextButton back;
private TextButton saveslot1;
private TextButton saveslot2;
private TextButton saveslot3;
private TextButton loadslot1;
private TextButton loadslot2;
private TextButton loadslot3;
private Window ww;
private Window storywindow;
private Window newgamesaveslotwindow;
private Window loadgameloadslotwindow;
private static float time = 0;
private float temp;



public MainMenuScreen(DugManMainClass game)
{
    this.game = game;
}

@Override
public void dispose() {

    Gdx.app.log(MainMenuScreen.LOG, "Disposing...");
    batch.dispose();
    texture.dispose();
    stage.dispose();

}

@Override
public void resize(int width, int height) {
}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void render(float delta) {

    Gdx.app.log(MainMenuScreen.LOG, "Rendering...");


    temp += Gdx.graphics.getDeltaTime();
    while(temp>1.0f/60.0f)
    {
        MainMenuScreen.tick();
        temp -=1.0f/60.0f;
    }

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stage.act(Gdx.graphics.getDeltaTime());

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    batch.disableBlending();
    sprite.draw(batch);
    batch.end();

    batch.begin();
    batch.enableBlending();
    stage.draw();
    batch.end();
    // TODO Auto-generated method stub
    }

@Override
public void show() {


    Gdx.app.log(MainMenuScreen.LOG, "Showing SplashScreen...");

    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    camera = new OrthographicCamera(1, h/w);
    //(x,y)
    //camera = new 
    batch = new SpriteBatch();

    stage = new Stage(w,h,true);

    Assets.bgsound.play(1f);

    Gdx.input.setInputProcessor(stage);

    stage.clear();

    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    TextureRegion region = new TextureRegion(texture, 0, 0, texture.getWidth(), texture.getHeight());

    sprite = new Sprite(region);
    sprite.setSize(1f, 1f * sprite.getHeight() / sprite.getWidth());
    sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
    sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2);

    Skin skin = new Skin(Assets.atlas);

    TextButtonStyle ts = new TextButtonStyle();
    ts.up = skin.getDrawable("btnpressed");
    ts.down = skin.getDrawable("btnunpressed");
    ts.font = Assets.menufont2;

    //main menu
    tb = new TextButton("Story mode", ts);
    tb.padTop(5).padBottom(5).padLeft(5).padRight(5);
    tb.addListener(new InputListener() 
    {

            public boolean touchDown(InputEvent event, float x, float y,
                            int pointer, int button) {
                    return true;
            }

            public void touchUp(InputEvent event, float x, float y,
                            int pointer, int button) {

                    time = 0;
                    storywindow.setVisible(true);
                    ww.setVisible(false);
                    Assets.pressedsound.play(1f);


            }
    });


    tb2 = new TextButton("Arcade", ts);
    tb2.padTop(5).padBottom(5).padLeft(5).padRight(5);
    tb2.addListener(new InputListener() 
    {

            public boolean touchDown(InputEvent event, float x, float y,
                            int pointer, int button) {
                    return true;
            }

            public void touchUp(InputEvent event, float x, float y,
                            int pointer, int button) {
                    Assets.pressedsound.play(1f);

            }
    });

    tb3 = new TextButton("Time Trial", ts);
    tb3.padTop(5).padBottom(5).padLeft(5).padRight(5);
    tb3.addListener(new InputListener() 
    {

            public boolean touchDown(InputEvent event, float x, float y,
                            int pointer, int button) {
                    return true;
            }

            public void touchUp(InputEvent event, float x, float y,
                            int pointer, int button) {
                time = 0;
                    Assets.pressedsound.play(1f);

            }
    });

    tb4 = new TextButton("Option",ts);
    tb4.padTop(5).padBottom(5).padLeft(5).padRight(5);
    tb4.addListener(new InputListener() 
    {

            public boolean touchDown(InputEvent event, float x, float y,
                            int pointer, int button) {
                    return true;
            }

            public void touchUp(InputEvent event, float x, float y,
                            int pointer, int button) {

                    Assets.pressedsound.play(1f);

            }
    });


    tb5 = new TextButton("Exit",ts);
    tb5.padTop(5).padBottom(5).padLeft(5).padRight(5);
    tb5.addListener(new InputListener() 
    {

            public boolean touchDown(InputEvent event, float x, float y,
                            int pointer, int button) {
                    return true;
            }

            public void touchUp(InputEvent event, float x, float y,
                            int pointer, int button) {
                    Assets.pressedsound.play(1f);
                    Gdx.app.exit();

            }
    });
    //storymode-newgame
    newgame = new TextButton("New Game", ts);
    newgame.padTop(5).padBottom(5).padLeft(5).padRight(5);
    newgame.addListener(new InputListener() 
     {

        public boolean touchDown(InputEvent event, float x, float y,
                        int pointer, int button) {
                return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                        int pointer, int button) {
                newgamesaveslotwindow.setVisible(true);
                storywindow.setVisible(false);
                Assets.pressedsound.play(1f);

        }
     });

    //newgamesaveslot
    saveslot1 = new TextButton("Save slot 1", ts);
    saveslot1.padTop(5).padBottom(5).padLeft(5).padRight(5);
    saveslot1.addListener(new InputListener() 

     {

        public boolean touchDown(InputEvent event, float x, float y,
                        int pointer, int button) {
                return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                        int pointer, int button) {
                game.setScreen(new LoadingScreen(game));
                Assets.ekek.play(1f);
                Assets.bgsound.stop();

        }
     });

    saveslot2 = new TextButton("Save slot 2", ts);
    saveslot2.padTop(5).padBottom(5).padLeft(5).padRight(5);
    saveslot2.addListener(new InputListener() 
     {

        public boolean touchDown(InputEvent event, float x, float y,
                        int pointer, int button) {
                return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                        int pointer, int button) {
                game.setScreen(new DugManGameScreen(game));
                Assets.ekek.play(1f);
                Assets.bgsound.stop();

        }
     });

    saveslot3 = new TextButton("Save slot 3", ts);
    saveslot3.padTop(5).padBottom(5).padLeft(5).padRight(5);
    saveslot3.addListener(new InputListener() 
     {

        public boolean touchDown(InputEvent event, float x, float y,
                        int pointer, int button) {
                return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                        int pointer, int button) {
                game.setScreen(new DugManGameScreen(game));
                Assets.ekek.play(1f);
                Assets.bgsound.stop();

        }
     });

    //storymode-loadgames
    load = new TextButton("Load game", ts);
    load.padTop(5).padBottom(5).padLeft(5).padRight(5);
    load.addListener(new InputListener() 

     {

        public boolean touchDown(InputEvent event, float x, float y,
                        int pointer, int button) {
                return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                        int pointer, int button) {
                loadgameloadslotwindow.setVisible(true);
                ww.setVisible(false);
                storywindow.setVisible(false);
                Assets.pressedsound.play(1f);


        }
     });

    //loadsaveslot
    loadslot1 = new TextButton("load slot 1", ts);
    loadslot1 .padTop(5).padBottom(5).padLeft(5).padRight(5);
    loadslot1 .addListener(new InputListener() 

     {

        public boolean touchDown(InputEvent event, float x, float y,
                        int pointer, int button) {
                return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                        int pointer, int button) {
                game.setScreen(new DugManGameScreen(game));
                Assets.pressedsound.play(1f);
                Assets.bgsound.stop();

        }
     });

    loadslot2 = new TextButton("load slot 2", ts);
    loadslot2 .padTop(5).padBottom(5).padLeft(5).padRight(5);
    loadslot2 .addListener(new InputListener() 
     {

        public boolean touchDown(InputEvent event, float x, float y,
                        int pointer, int button) {
                return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                        int pointer, int button) {
                game.setScreen(new DugManGameScreen(game));
                Assets.pressedsound.play(1f);
                Assets.bgsound.stop();

        }
     });
    loadslot3 = new TextButton("load slot 3", ts);
    loadslot3 .padTop(5).padBottom(5).padLeft(5).padRight(5);
    loadslot3 .addListener(new InputListener() 
     {

       public boolean touchDown(InputEvent event, float x, float y,
                       int pointer, int button) {
               return true;
       }

       public void touchUp(InputEvent event, float x, float y,
                       int pointer, int button) {
            game.setScreen(new DugManGameScreen(game));
            Assets.pressedsound.play(1f);
            Assets.bgsound.stop();

       }
     });

    back = new TextButton("back", ts);
    back.padTop(5).padBottom(5).padLeft(5).padRight(5);
    back.addListener(new InputListener() 
     {

        public boolean touchDown(InputEvent event, float x, float y,
                        int pointer, int button) {
                return true;
        }

        public void touchUp(InputEvent event, float x, float y,
                        int pointer, int button) {


            if(storywindow.isVisible()==true)
            {
                ww.setVisible(true);
                storywindow.setVisible(false);
                 Assets.pressedsound.play(1f);
            }
            else if(newgamesaveslotwindow.isVisible()==true)
            {
                ww.setVisible(false);
                storywindow.setVisible(true);
                newgamesaveslotwindow.setVisible(false);
                 Assets.pressedsound.play(1f);
            }
            else if(loadgameloadslotwindow.isVisible()==true)
            {
                ww.setVisible(false);
                storywindow.setVisible(true);
                loadgameloadslotwindow.setVisible(false);
                 Assets.pressedsound.play(1f);
            }




        }

     });




    WindowStyle www = new WindowStyle();
    www.background = skin.getDrawable("windowmenu1");
    www.titleFont = Assets.menufont2;

    ww = new Window("", www);
    ww.setPosition(0,0);
    ww.setY(10);
    ww.setX(260);

    ww.defaults().spaceBottom(10);
    ww.padTop(25);
    ww.padLeft(50);
    ww.padRight(50);
    ww.row().fill().expandX();
    ww.add(tb);
    ww.row().fill().expandX();
    ww.add(tb2);
    ww.row().fill().expandX();
    ww.add(tb3);
    ww.row().fill().expandX();
    ww.add(tb4);
    ww.row().fill().expandX();
    ww.add(tb5);
    ww.padBottom(25);
    ww.pack();

    storywindow = new Window("",www);
    storywindow.setPosition(0, 0);
    storywindow.setY(10);
    storywindow.setX(260);
    storywindow.defaults().spaceBottom(10);
    storywindow.padTop(25);
    storywindow.padLeft(50);
    storywindow.padRight(50);
    storywindow.row().fill().expandX();
    storywindow.add(newgame);
    storywindow.row();
    storywindow.add(load);
    storywindow.row();
    storywindow.add(back);
    storywindow.padBottom(25);
    storywindow.pack();
    storywindow.setVisible(false);

    newgamesaveslotwindow = new Window("",www);
    newgamesaveslotwindow.setPosition(0, 0);
    newgamesaveslotwindow.setY(10);
    newgamesaveslotwindow.setX(260);
    newgamesaveslotwindow.defaults().spaceBottom(10);
    newgamesaveslotwindow.padTop(25);
    newgamesaveslotwindow.padLeft(50);
    newgamesaveslotwindow.padRight(50);
    newgamesaveslotwindow.row();
    newgamesaveslotwindow.add(saveslot1);
    newgamesaveslotwindow.row();
    newgamesaveslotwindow.add(saveslot2);
    newgamesaveslotwindow.row();
    newgamesaveslotwindow.add(saveslot3);
    newgamesaveslotwindow.row();
    newgamesaveslotwindow.add(back);
    newgamesaveslotwindow.padBottom(25);
    newgamesaveslotwindow.pack();
    newgamesaveslotwindow.setVisible(false);

    loadgameloadslotwindow = new Window("",www);
    loadgameloadslotwindow.setPosition(0, 0);
    loadgameloadslotwindow.setY(10);
    loadgameloadslotwindow.setX(260);
    loadgameloadslotwindow.defaults().spaceBottom(10);
    loadgameloadslotwindow.padTop(25);
    loadgameloadslotwindow.padLeft(50);
    loadgameloadslotwindow.padRight(50);
    loadgameloadslotwindow.row().fill().expandX();
    loadgameloadslotwindow.add(loadslot1);
    loadgameloadslotwindow.row().fill().expandX();
    loadgameloadslotwindow.add(loadslot2);
    loadgameloadslotwindow.row().fill().expandX();
    loadgameloadslotwindow.add(loadslot3);
    loadgameloadslotwindow.row().fill().expandX();
    loadgameloadslotwindow.add(back);
    loadgameloadslotwindow.padBottom(25);
    loadgameloadslotwindow.pack();
    loadgameloadslotwindow.setVisible(false);


    stage.addActor(storywindow);
    stage.addActor(newgamesaveslotwindow);
    stage.addActor(loadgameloadslotwindow);
    stage.addActor(ww);

}

public static void tick() {

    time++;

    if (time > 60 * 10) {

        game.setScreen(new NewPreviewScreen(game));
        Assets.bgsound.stop();

    }


}

}

as for my other problem which I have encountered a unresponsive screen, this is my code for previewscreen.

public class PreviewScreen implements Screen {

private static DugManMainClass game;
private SpriteBatch batch;
private OrthographicCamera camera;
private Texture tx1 = Assets.bgmenu;
private BitmapFont font = Assets.menufont2;
private final static List<String> lines = new ArrayList<String>(); 
private static int time = 0;
private float temp = 0;
private Sprite s;


public PreviewScreen(DugManMainClass game) {

    this.game = game;

    try
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(Gdx.files.internal("previewtext.txt").read()));
        String line = "";
        while ((line = br.readLine()) != null) {
            lines.add(line);
        }
        br.close();
    } 
    catch(Exception e)
    {
        e.printStackTrace();
    }


}


@Override
public void render(float delta) {
    float w = s.getHeight();

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    temp += Gdx.graphics.getDeltaTime();


    while(temp>1.0f/60.0f)
    {
        PreviewScreen.tick();
        temp -=1.0f/60.0f;

        s.setU(temp+1f);
        s.setU2(temp+1f);

    }





    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    s.draw(batch);

    int yo = time / 4;



    for (int y = 0; y <= s.getHeight() / 12; y++) 
    {
        int yl = yo / 12 + 420 / 12 - y;
        if (yl >= 0 && yl < lines.size()) 
        {
            font.draw(batch,lines.get(yl), (800 - 30 * 12) / 2,  y * 12 + yo % 12);

        }
    }

    batch.end();



}


@Override
public void show() {

    batch = new SpriteBatch();

    float w = 800;
    float h = 420;

    camera = new OrthographicCamera();
    camera.setToOrtho(false, w, h);

    tx1.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);

    s = new Sprite(tx1);
    s.setPosition(0, 0);


    Assets.bgsound3.play(1f);
    // TODO Auto-generated method stub

}


@Override
public void dispose() {

    Assets.dispose();

}

public static void tick() {
    time++;

    if (Gdx.input.isKeyPressed(Keys.ENTER) || Gdx.input.isTouched()) {
        game.setScreen(new splashscreen(game));
        Assets.bgsound3.stop();
    }

    if (time / 4 > lines.size() * 10 + 250) {


        game.setScreen(new splashscreen(game));
        Assets.bgsound3.stop();
    }

}

}

when it receives an input or finishes displaying the texts, it will set the screen to splashscreen and after the splash it will proceed to the intro and mainmenu, vice versa.

Upvotes: 1

Views: 684

Answers (1)

Jyro117
Jyro117

Reputation: 4529

I see a few things which are concerning, but first I will focus on the issue at hand:

private static int time = 0;

You are using a static variable in your screens and only seem to reset them to 0 when you press a button on the MainMenuScreen. This means the next time you display the screen, the value will result in a screen switch and it will flip back and forth forever (and cause a lot of lag).

Other issues:

You are storing a considerable amount of static state in your code, this is really frowned upon. Here is a good post (refer to section on implications) which talks about issues related to it. Most programming guides would advise against it.

Consider looking into better variable names, or even using arrays instead of numbering variables. It will simplify your code and make it more readable.

You should also consider re-factoring/delegating code outside of the screen classes. This way you are separating concerns and not putting everything into your classes. It isn't an issue now, but it will quickly become one as you expand your game.

Upvotes: 2

Related Questions