2015-05-19 89 views
0

我需要你的幫助,一個簡單的問題。關閉暫停菜單,並返回遊戲畫面 - Java遊戲與LIBGDX

我使用LibGDX庫實現了我的Java遊戲。我的遊戲有以下結構: - 主菜單有一些按鈕; - 遊戲畫面; - 當用戶按P關鍵字時暫停menù打開。

問題是這樣的。當我按P時,暫停菜單打開。如果我在暫停菜單中單擊MainMenù按鈕,屏幕將繼續主菜單。之後,如果我按下游戲按鈕,它會暫停菜單屏幕而不是遊戲屏幕。

我該如何解決這個問題?


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() { 

} 
} 
+0

OK @donfuxx你能幫助我與我的問題? :( – Agata

+0

你正在做的很多東西里面的渲染方法!請注意,這種方法每秒被調用約60次。 – donfuxx

+0

@ donfuxx好了我會在稍後修復代碼,我現在關心的是要解決這個問題,請 – Agata

回答

0

我知道這是一歲,但我回答呢。

不要在渲染的每一幀中創建菜單項。創建一次。

public class GameScreen implements Screen{ 

private State state; //this keeps track of where 


public static enum State{ 
    RUNNING, PAUSE, SAVEGAME 
} 


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

} 

@Override 
public void show() { 
    //do the loading and creating here 
} 


@Override 
public void render(float delta) { 

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

    switch(state){ 
     case RUNNING: running(); 
         break; 

     case PAUSE:  pause(); 
         break; 

     case SAVEGAME: savegame(); 
         break; 

     default:  break; 
    } 

} 


private void running(){ 

} 

private void pause(){ 

} 

private void savegame(){ 

} 

}