关闭暂停menù并回到游戏画面 - 与LIBGDX的java游戏

时间:2015-05-19 18:31:00

标签: java libgdx

我需要你帮助解决一个简单的问题。

我使用LibGDX库实现了我的Java游戏。 我的游戏有以下结构: - 主要的menù有一些按钮; - 游戏画面; - 当用户按P关键字时暂停menù。

问题就是这个。当我按P时,暂停menù打开。如果我在暂停菜单中单击MainMenù按钮,屏幕将显示主菜单。之后,如果我按下游戏按钮,它会显示暂停menù屏幕而不是游戏屏幕。

我该如何解决这个问题?

public class MenuPause implements Screen {
private Skin skin;
private Stage stage;
private MyGdxGame game;
private Level level=new Level(true);    
private World world;
private GameScreen8Bit GS;
private int era;

/*****Button *******/
private Button saveGame;
private Button loadGame;
private Button continueGame;
private Button exitGame;
private Button mainMenu;
/******************/


private SpriteBatch batch;
private TextureAtlas atlas;
private TextureRegion imagetexture;


public MenuPause(MyGdxGame game, int era){
    this.game=game;

}
private void createBasicSkin(){
      //Create a font

    atlas = new TextureAtlas(Gdx.files.internal("images/textures/game.atlas"));
      BitmapFont font = new BitmapFont();
      skin = new Skin();
      skin.add("default", font);

      //Create a texture
      Pixmap pixmap = new Pixmap((int)Gdx.graphics.getWidth()/4,(int)Gdx.graphics.getHeight()/10, Pixmap.Format.RGB888);
      pixmap.setColor(Color.WHITE);
      pixmap.fill();
      skin.add("background",new Texture(pixmap));

      //Create a button style
      TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
      textButtonStyle.up = skin.newDrawable("background", Color.GRAY);
      textButtonStyle.down = skin.newDrawable("background", Color.DARK_GRAY);
      textButtonStyle.checked = skin.newDrawable("background", Color.DARK_GRAY);
      textButtonStyle.over = skin.newDrawable("background", Color.LIGHT_GRAY);
      textButtonStyle.font = skin.getFont("default");
      skin.add("default", textButtonStyle);

    }


@Override
public void render(float delta) {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    Vector3 touchPos = new Vector3();

    touchPos.set((Gdx.input.getX()), (Gdx.graphics.getHeight()-Gdx.input.getY()), 0);

    if(exitGame.mousePassage(touchPos))
    {
        exitGame=new Button(atlas.findRegion("exitNoPressed"));
        exitGame.setPos(0, 0);
    }
    else
    {
        exitGame=new Button(atlas.findRegion("exit"));
        exitGame.setPos(0, 0);
    }
    if(loadGame.mousePassage(touchPos))
    {
        loadGame=new Button(atlas.findRegion("loadNoPressed"));
        loadGame.setPos(Gdx.graphics.getWidth()/2-atlas.findRegion("loadNoPressed").getRegionWidth()/2, (Gdx.graphics.getHeight()/2));

    }
    else
    {
        loadGame=new Button(atlas.findRegion("load"));
        loadGame.setPos(Gdx.graphics.getWidth()/2-atlas.findRegion("load").getRegionWidth()/2, (Gdx.graphics.getHeight()/2));
    }
    if(saveGame.mousePassage(touchPos))
    {
        saveGame=new Button(atlas.findRegion("saveNoPressed"));
         saveGame.setPos(0 , Gdx.graphics.getHeight()/3);
    }
    else
    {
        saveGame=new Button(atlas.findRegion("save"));
         saveGame.setPos(0 , Gdx.graphics.getHeight()/3);
    }
    if(continueGame.mousePassage(touchPos))
    {
        continueGame=new Button(atlas.findRegion("continueNoPressed"));
        continueGame.setPos((Gdx.graphics.getWidth() - atlas.findRegion("continueNoPressed").getRegionWidth()) , 0);

    }
    else
    {
        continueGame=new Button(atlas.findRegion("continue"));
        continueGame.setPos((Gdx.graphics.getWidth() - atlas.findRegion("continue").getRegionWidth()) , 0);

    }
    if(mainMenu.mousePassage(touchPos))
    {
        mainMenu=new Button(atlas.findRegion("mainNoPressed"));
        mainMenu.setPos(Gdx.graphics.getWidth()/2-atlas.findRegion("mainNoPressed").getRegionWidth()/2 , 0);

    }
    else
    {
        mainMenu=new Button(atlas.findRegion("main"));
        mainMenu.setPos((Gdx.graphics.getWidth()/2-atlas.findRegion("main").getRegionWidth()/2) , 0);

    }


     if(Gdx.input.isButtonPressed(Input.Buttons.LEFT)){



         if(exitGame.isPressed(touchPos)){
            int n= JOptionPane.showConfirmDialog(null,"Sei sicuro di volere uscire dal gioco?", "EXIT", JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
            if(n==0)
            {
                System.exit(0); 
            }
        }

        if(loadGame.isPressed(touchPos)){
            new LoadGame(level,false);
        }


        if(saveGame.isPressed(touchPos)){
            SavePanel savePanel = new SavePanel( level, game,false);
            savePanel.save();
        }
        if(mainMenu.isPressed(touchPos)){
            dispose();
            this.dispose();
            game.setScreen(new MainMenu(game,0));

        }
        if(continueGame.isPressed(touchPos)){
            //game.setScreen(new MainMenu(game));//mainMenu non funziona...eccezione
            //game.setScreen(new GameScreen8Bit(game,level));
        }
    }


   batch.begin();
    batch.draw(imagetexture,0 , 0);
    saveGame.draw(batch);
    loadGame.draw(batch);
    continueGame.draw(batch);
    exitGame.draw(batch);
    mainMenu.draw(batch);
    batch.end();
}

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

@Override
public void show() {
    batch = new SpriteBatch();
    createBasicSkin();
    saveGame= new Button(atlas.findRegion("save"));
    saveGame.setPos(0 , Gdx.graphics.getHeight()/3);
    loadGame=new Button(atlas.findRegion("load"));
    loadGame.setPos(Gdx.graphics.getWidth()/2-atlas.findRegion("load").getRegionWidth()/2, (Gdx.graphics.getHeight()/2));

    continueGame=new Button(atlas.findRegion("continue"));
    continueGame.setPos((Gdx.graphics.getWidth() - atlas.findRegion("continue").getRegionWidth()) , 0);

    exitGame=new Button(atlas.findRegion("exit"));
    exitGame.setPos(0, 0);

    mainMenu=new Button(atlas.findRegion("main"));
    mainMenu.setPos(Gdx.graphics.getWidth()/2-atlas.findRegion("load").getRegionWidth()/2, 0);

    Texture immagine=new Texture(Gdx.files.internal("data/ghiacciairid.png"));


    if(era==1)
    {
        immagine=new Texture(Gdx.files.internal("data/8bitrid.png"));
    }
    else if(era==2)
    {
        immagine=new Texture(Gdx.files.internal("data/16bitrid.png"));
    }
    else if(era==3)
    {
        immagine=new Texture(Gdx.files.internal("data/3d.png"));
    }
    imagetexture =new TextureRegion(immagine,640,480);
    imagetexture.setRegionHeight(480);
    imagetexture.setRegionWidth(640);
}
@Override
public void hide() {
    dispose();
}

@Override
public void pause() {
}

@Override
public void resume() {
}

@Override
public void dispose() {

 }
}

1 个答案:

答案 0 :(得分:0)

我知道这已经有一年了,但无论如何我都在回答。

不要在渲染中每帧创建菜单项。创建一次。

import {ACCORDION_DIRECTIVES} from "ng2-accordion/ng2-accordion";

}