2014-06-26 34 views
1
public class PlayScreen implements Screen{ 

    Stage stage; 

    LabelStyle style; 
    BitmapFont font; 

    TextureAtlas backbuttonatlas; 
    TextButtonStyle backbuttonstyle; 
    TextButton backbutton; 
    Skin backskin; 

    SpriteBatch batch; 
    Texture pibe; 
    Sprite sprite; 
    Vector2 position; 
    Game game; 

    Texture texture; 

    public PlayScreen(Game game){ 
     this.game=game; 
    } 

    @Override 
    public void render(float delta) { 

    stage=new Stage(); 

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


    if(Gdx.input.isKeyPressed(Keys.W)) 
    { 
     position.x+=5f; 
    } 
    if(Gdx.input.isKeyPressed(Keys.A)) 
    { 
     position.y-=5f; 
    } 
    if(Gdx.input.isKeyPressed(Keys.S)) 
    { 
     position.x-=5f; 
    } 
    if(Gdx.input.isKeyPressed(Keys.D)) 
    { 
     position.y+=5f; 
    } 

    if(Gdx.input.isTouched()==true) 
    { 
     if(Gdx.input.getY()>Gdx.graphics.getHeight()/2) 
     { 
     position.x-=5; 
     } 
     if(Gdx.input.getY()<Gdx.graphics.getHeight()/2) 
     { 
      position.x+=5; 
     } 
     if(Gdx.input.getX()>Gdx.graphics.getWidth()/2) 
     { 
      position.y+=5; 
     } 
     if(Gdx.input.getX()<Gdx.graphics.getWidth()/2) 
     { 
      position.y-=5; 
     } 

     if(Gdx.input.isKeyPressed(Keys.BACK)) 
     { 
      game.setScreen(new MainMenu(game)); 
     } 
    } 

    font = new BitmapFont(Gdx.files.internal("font.fnt"), false); 
    style = new LabelStyle(font, Color.WHITE); 

    backskin = new Skin(); 
    backbuttonatlas = new TextureAtlas("buttons/backbutton.pack"); 
    backskin.addRegions(backbuttonatlas); 

    backbuttonstyle = new TextButtonStyle(); 
    backbuttonstyle.up = backskin.getDrawable("backbutton"); 
    backbuttonstyle.over = backskin.getDrawable("backbuttonpressed"); 
    backbuttonstyle.down = backskin.getDrawable("backbuttonpressed"); 
    backbuttonstyle.font = font; 

    backbutton = new TextButton("", backbuttonstyle); 
    backbutton.setWidth((float) (Gdx.graphics.getHeight()/8)); 
    backbutton.setHeight((float) (Gdx.graphics.getHeight()/8)); 
    backbutton.setPosition((Gdx.graphics.getWidth()/20), (float) (Gdx.graphics.getHeight()-(Gdx.graphics.getWidth()/8))); 

    backbutton.addListener(new InputListener(){ 

     public boolean touchDown(InputEvent event, float x, float y, int pointer, int backbutton) { 
       game.setScreen(new MainMenu(game)); 
      return true; 
     };}); 

    batch=new SpriteBatch(); 

    stage.addActor(backbutton); 

    Gdx.input.setInputProcessor(stage); 

    batch.begin(); 
    batch.draw(texture, 0, 0, Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); 
    batch.draw(pibe,(position.y/2-pibe.getWidth()/2),(position.x/2-pibe.getHeight()/2)); 
    batch.end(); 

    stage.act(); 
    stage.draw(); 

    } 

    @Override 
    public void resize(int width, int height) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void show() { 
     texture = new Texture("cielo.png"); 

     pibe = new Texture("superman (100x52).jpg"); 
     position = new Vector2(Gdx.graphics.getHeight(),Gdx.graphics.getWidth()); 
    } 

    @Override 
    public void hide() { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void pause() { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void resume() { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void dispose() { 
     // TODO Auto-generated method stub 
    } 
} 

我LibGDX遊戲,倒塌幾分鐘後,我不知道爲什麼。我已經讀了一些關於這個問題的文章,並且說解決方案是「處理」bitmapfont,或者類似的東西。我是LibGDX的新手,我不太瞭解。完整的解釋表示讚賞。對不起,我英文很差。 這是Play類。請,需要幫助。謝謝。5分鐘後的磨合,我libgdx遊戲崩潰,然後呈紅色

+0

對於那些明顯需要幫助代碼的人,我不會得到滿意的結果。 Upvoted。 –

回答

1

每次設備準備好更新屏幕時都會調用渲染。您正在每幀創建新對象。其中一些必須手動處理,這意味着調用該對象的方法.dispose()

呼叫

font.dispose(); 

當你用的字體完成,以防止它吃了所有的記憶。

理想情況下,您希望在渲染循環之外創建該字體。

您應該在構造函數中創建對象,以便它們不會在每一幀中重新創建。除非這是預期的行爲。

嘗試是這樣的

public class PlayScreen implements Screen{ 

    Stage stage; 

    LabelStyle style; 
    BitmapFont font; 

    TextureAtlas backbuttonatlas; 
    TextButtonStyle backbuttonstyle; 
    TextButton backbutton; 
    Skin backskin; 

    SpriteBatch batch; 
    Texture pibe; 
    Sprite sprite; 
    Vector2 position; 
    Game game; 

    Texture texture; 

    public PlayScreen(Game game){ 
     this.game=game; 
     font = new BitmapFont(Gdx.files.internal("font.fnt"), false); 
     style = new LabelStyle(font, Color.WHITE); 
     stage=new Stage(); 
     backskin = new Skin(); 
     backbuttonatlas = new TextureAtlas("buttons/backbutton.pack"); 
     backskin.addRegions(backbuttonatlas); 

     backbuttonstyle = new TextButtonStyle(); 
     backbuttonstyle.up = backskin.getDrawable("backbutton"); 
     backbuttonstyle.over = backskin.getDrawable("backbuttonpressed"); 
     backbuttonstyle.down = backskin.getDrawable("backbuttonpressed"); 
     backbuttonstyle.font = font; 

     backbutton = new TextButton("", backbuttonstyle); 
     backbutton.setWidth((float) (Gdx.graphics.getHeight()/8)); 
     backbutton.setHeight((float) (Gdx.graphics.getHeight()/8)); 
     backbutton.setPosition((Gdx.graphics.getWidth()/20), (float) (Gdx.graphics.getHeight()-(Gdx.graphics.getWidth()/8))); 

     backbutton.addListener(new InputListener(){ 

      public boolean touchDown(InputEvent event, float x, float y, int pointer, int backbutton) { 
        game.setScreen(new MainMenu(game)); 
       return true; 
      };}); 

     batch=new SpriteBatch(); 

     stage.addActor(backbutton); 

    } 

    @Override 
    public void render(float delta) { 


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


     if(Gdx.input.isKeyPressed(Keys.W)) 
     { 
      position.x+=5f; 
     } 
     if(Gdx.input.isKeyPressed(Keys.A)) 
     { 
      position.y-=5f; 
     } 
     if(Gdx.input.isKeyPressed(Keys.S)) 
     { 
      position.x-=5f; 
     } 
     if(Gdx.input.isKeyPressed(Keys.D)) 
     { 
      position.y+=5f; 
     } 

     if(Gdx.input.isTouched()==true) 
     { 
      if(Gdx.input.getY()>Gdx.graphics.getHeight()/2) 
      { 
      position.x-=5; 
      } 
      if(Gdx.input.getY()<Gdx.graphics.getHeight()/2) 
      { 
       position.x+=5; 
      } 
      if(Gdx.input.getX()>Gdx.graphics.getWidth()/2) 
      { 
       position.y+=5; 
      } 
      if(Gdx.input.getX()<Gdx.graphics.getWidth()/2) 
      { 
       position.y-=5; 
      } 

      if(Gdx.input.isKeyPressed(Keys.BACK)) 
      { 
       game.setScreen(new MainMenu(game)); 
      } 
     } 

     Gdx.input.setInputProcessor(stage); 

     batch.begin(); 
     batch.draw(texture, 0, 0, Gdx.graphics.getWidth(),Gdx.graphics.getHeight()); 
     batch.draw(pibe,(position.y/2-pibe.getWidth()/2),(position.x/2-pibe.getHeight()/2)); 
     batch.end(); 

     stage.act(); 
     stage.draw(); 

    } 

    @Override 
    public void resize(int width, int height) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void show() { 
     texture = new Texture("cielo.png"); 

     pibe = new Texture("superman (100x52).jpg"); 
     position = new Vector2(Gdx.graphics.getHeight(),Gdx.graphics.getWidth()); 
    } 

    @Override 
    public void hide() { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void pause() { 
     // TODO Auto-generated method stub 
    } 
+0

嗨。首先,需要努力。當我將所有對象的創建移動到構造函數時,它會要求我更改:'公共PlayScreen(遊戲遊戲)'。公共PlayScreen(最終遊戲遊戲)'。我該怎麼辦?。另外,如果我移動這些objets,我遇到了一個問題:'stage.addActor(backbutton);'。它說:線程中的異常「LWJGL應用程序」... – Nicocoen97

4
你必須把你的 「創造」 像 batch = new SpriteBatch()裏面的東西

@覆蓋 公共無效創建(

){

你創建數十億個導致內存問題的SpriteBatches。

+0

同意。 OP幾乎可以確保非常快速地堆滿堆。 –