2015-04-14 2 views
0

Я пытаюсь нарисовать текст на своих кнопках для меню «Пуск».Проблема с выпуском Libgdx

Я могу нарисовать кнопки и все, кроме шрифта/текста, просто отлично.

Вот код шрифта:

Initializing:

buttonFont = new BitmapFont(Gdx.files.internal(
           "StartMenu/gamefont.fnt"), false); 

    buttonFont.setScale(0.2f); 

Вот где я рисую сцену, сцена имеет шрифт внутри него:

Gdx.gl20.glClearColor(1, 0, 0, 1); 

    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT); 

    stage.act(delta); 

    sb.begin(); 

    stage.draw(); 

    sb.end(); 

Это материал для инициализации еще нескольких вещей:

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

    if(stage == null) 
     stage = new Stage(); 

     stage.clear(); 

     Gdx.input.setInputProcessor(stage); 

     style.font = buttonFont; 

     style.up = skin.getDrawable("Button"); 

     style.down = skin.getDrawable("Button"); 

     startGameBtn = new TextButton("Start Game", style); 

     startGameBtn.setWidth(250); 

     startGameBtn.setHeight(150); 

     startGameBtn.setX(Gdx.graphics.getWidth()/2 - startGameBtn.getWidth()/2); 

     startGameBtn.setY((float) (Gdx.graphics.getHeight()/1.5)); 

     startGameBtn.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 Gameplay(game)); 
      } 


     }); 

     stage.addActor(startGameBtn); 

     stage.addActor(lb); 

    } 

Любая помощь очень ценится! :)

+0

Есть ли причина, по которой вы не используете 'TextButton'? – Springrbua

+0

Я использую textbuttons – Dumbostyle

+0

Извините, не видел, что ... Какую версию Libgdx вы используете? Похоже, что они изменили что-то относительно BitmapFont в последней версии ... – Springrbua

ответ

0

Мне кажется странным, что вы создали свою кнопку в resize().

То, что я хотел бы предложить, и что работает для меня, инициализировать все в create(), рисовать сцену в render() и поставил Слушатель в show()

Initialization

create(){ 
    //Define the stage where you'll draw the TextButton 
    stage = new Stage(); 
    //Define your font 
    buttonFont = new BitmapFont(Gdx.files.internal("StartMenu/gamefont.fnt"), false); 
    //Define the skin that contains your images 
    textureAtlas = game.assets.get("Images.pack", TextureAtlas.class); 
    skin.addRegions(textureAtlas); 

    //Define the TextButtonStyle 
    textButtonStyle = new TextButtonStyle(); 
    textButtonStyle.up = skin.getDrawable("Button"); 
    textButtonStyle.down = skin.getDrawable("Button"); 
    textButtonStyle.font = buttonFont; 
    textButtonStyle.fontColor = Color.WHITE; 
    textButtonStyle.downFontColor = new Color(0.5f, 0.5f, 0.5f, 1); 

    //Define the TextButton 
    startGameBtn = new TextButton("Start Game", textButtonStyle); 
    startGameBtn.setWidth(250); 
    startGameBtn.setHeight(150); 
    startGameBtn.setX(Gdx.graphics.getWidth()/2 - startGameBtn.getWidth()/2); 
    startGameBtn.setY(Gdx.graphics.getHeight()/1.5f); 

    //Add the TextButton to the stage 
    stage.addActor(startGameBtn); 
} 

Нанесение TextButton

render(){ 
    Gdx.gl20.glClearColor(1, 0, 0, 1); 
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    stage.act(); 
    stage.draw(); 
} 

Использование TextButton

show(){ 
    startGameBtn.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 Gameplay(game)); 
     } 
    }); 
}