こんにちは、私は多くの画面 (イントロ画面、スプラッシュ画面、プレビュー画面、メインメニュー画面など) を使用するゲームの開発を開始しましたが、次の問題のいくつかに遭遇しました:
画面から別の画面への切り替えは、応答しないかバグが多い場合があり、同じ画面が何度も繰り返される場合があります。別のウィンドウで使用する予定だった戻るボタンをリサイクルしましたが、一方のウィンドウでのみ表示され、もう一方のウィンドウでは表示されます。私は自分の画面を SCREENS に実装していますが、これは抽象画面クラスで何かを行う必要がありますか? さまざまなサンプル コードで見たように、画面を実装する特定のクラスに画面を拡張するためです。
この部分はかなり混乱しています。この問題の適切な解決策が見つかることを願っています。
適切にナビゲートしたり、画面の応答性を高める方法はありますか?スクリーンマネージャーを作成すると、ゲームのパフォーマンスが向上しますか?スクリーンマネージャーにはどのようなインスタンスが必要ですか?
これは、戻るボタンで問題が発生した mainmenuscreen のコードです。
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();
}
}
}
応答しない画面に遭遇した他の問題については、これがプレビュースクリーンのコードです。
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();
}
}
}
入力を受け取るか、テキストの表示を終了すると、画面をスプラッシュスクリーンに設定し、スプラッシュの後、イントロとメインメニューに進みます。逆も同様です。