android - Libgdx: screen managaer -
good day sirs, i've started develop game uses lot of screens(introscreen,splashscreen,previewscreen,mainmenuscreen, etc.) , have encountered of following problem:
switching screen other screen unresponsive or buggy, , repeats same screen on , on again. have recycled button planned on using in different windows, visible in 1 of windows in other. implements screens screens, have abstract screen class because have seen in different sample codes extend screen class implements screen.
i'm quite confuse part, hope find proper solution problem.
is there way navigate or make more responsive screens?would improve performance of game create screenmanager?what instances should screenmanager have?
this code mainmenuscreen encounters problem 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(); } }); = 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 other problem have encountered unresponsive screen, code 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; (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 receives input or finishes displaying texts, set screen splashscreen , after splash proceed intro , mainmenu, vice versa.
i see few things concerning, first focus on issue @ hand:
private static int time = 0;
you using static variable in screens , seem reset them 0 when press button on mainmenuscreen. means next time display screen, value result in screen switch , flip , forth forever (and cause lot of lag).
other issues:
you storing considerable amount of static state in code, frowned upon. here post (refer section on implications) talks issues related it. programming guides advise against it.
consider looking better variable names, or using arrays instead of numbering variables. simplify code , make more readable.
you should consider re-factoring/delegating code outside of screen classes. way separating concerns , not putting classes. isn't issue now, become 1 expand game.
Comments
Post a Comment