2013-07-23 144 views
1

好日子先生們,我已經開始開發一款遊戲,它使用了大量的屏幕(introscreen,閃屏,previewscreen,mainmenuscreen等),並遇到了一些着以下問題:Libgdx:屏幕managaer

從屏幕切換到其他屏幕有時無反應或有問題,有時會一遍又一遍地重複同一屏幕。 我已經回收了一個後退按鈕,我計劃在不同的窗口中使用該按鈕,但只能在其中一個窗口中看到,但在另一個窗口中可見。 我實現我的屏幕屏幕,這是否必須做任何事情與抽象屏幕類,因爲正如我已經看到在不同的示例代碼,他們將他們的屏幕擴展到實現屏幕的某個類。

我對這部分很迷惑,希望能找到適當的解決方案來解決這個問題。

有沒有一種方法可以正確導航或使其更響應我的屏幕?它是否有助於提高遊戲性能我將創建屏幕管理器?屏幕管理員應具有哪些實例?

這是我的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(); 

    } 


} 

}

爲我曾經遇到一個反應遲鈍的屏幕我的其他問題,這是我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(); 
    } 

} 

}當它接收到一個輸入或結束顯示所述文本

,它將設置屏幕以濺射屏幕和飛濺後它將繼續到介紹和MainMenu的,反之亦然。

+1

向我們展示您的代碼,我們將嘗試說明爲什麼其他屏幕有時無響應或錯誤或後退按鈕問題。對於後退按鈕,我現在可以想象的是,您首先繪製後退按鈕,然後繪製背景和其他內容。一世。e後退按鈕與屏幕上的其他紋理重疊。就我而言,所有這些問題都與您的代碼中實現SCREEN無關..其餘嘗試將您的代碼 – Pranav008

+0

@ pranav008請問先生。我試圖顯示我的代碼爲這個屏幕。反正後退按鈕,我也嘗試顯示代碼。 –

回答

2

我看這是有關的幾件事情,但首先我會重點考慮的問題,在手:

private static int time = 0; 

您正在使用您的屏幕靜態變量,似乎只是你什麼時候他們重置爲0按下MainMenuScreen上的按鈕。這意味着下次您顯示屏幕時,該值將導致屏幕切換,並且會永久地來回翻轉(並導致大量滯後)。

其他問題:

你存儲在你的代碼相當數量的靜態的,這實在是令人難以接受的。這裏有一個很好的post(參見關於含義的部分),其中討論與其相關的問題。大多數節目指南會建議反對它。

考慮查看better variable names,或者甚至使用數組代替編號變量。它將簡化您的代碼並使其更具可讀性。

您還應該考慮在屏幕類以外的代碼/委託代碼。這樣你就可以分離顧慮,而不是把所有東西都放到你的課堂上。這個問題現在不是問題,但當你擴展你的遊戲時,它很快就會成爲一個問題。