【问题标题】:Cannot draw TextButton or Button in LibGDX for desktop无法在桌面版 LibGDX 中绘制 TextButton 或 Button
【发布时间】:2014-05-21 11:11:34
【问题描述】:

我正在尝试在 LibGDX 中制作菜单。 但是我无法在屏幕上绘制 Button 或 textButton 。 我尝试使用测试皮肤。 我试着用一张桌子。 我尝试使用按钮和文本按钮。

图像很好,因为它可以在使用 SpriteBatch 绘制时出现,而且字体也很好。

这是我的代码的一部分。

public class Menu implements Screen {

    private Stage stage;
    private Skin skin;
    private TextButton textButton;
    private Game mygame;
    private BitmapFont font;
    private TextureAtlas atlas;
    private SpriteBatch batch;

    private TextureRegion test;

    public SplashScreen(Game g){
        mygame = g; 
    }

    @Override
    public void show() {
        batch = new SpriteBatch();
        stage = new Stage();
        stage.clear();
        atlas = new TextureAtlas("buttons/bot.pack");
        skin = new Skin(atlas);
        font = new BitmapFont(Gdx.files.internal("fonts/arialfnt.fnt"),false);
        TextButtonStyle buttonStyle = new TextButtonStyle();
        buttonStyle.up = skin.getDrawable("button.up");
        buttonStyle.over = skin.getDrawable("button.over");
        buttonStyle.down = skin.getDrawable("button.down");
        buttonStyle.font = font;

        textButton = new TextButton("Click here", buttonStyle);
        textButton.setPosition(200, 300);
        textButton.setHeight(300);
        textButton.setWidth(100); 

        test = atlas.findRegion("button.up"); //Testing the atlas
        stage.addActor(textButton);

    }

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

        stage.act(delta);
        stage.draw(); //DOES NOT WORK

        batch.begin();
        batch.draw(test, 300, 300); //It works
        font.draw(batch, "BLA BLA!", 50, 500); //It works
        batch.end();

        }

}

您对这项工作有什么建议吗? 会不会是GL3.0的原因?

【问题讨论】:

    标签: opengl libgdx opengl-3 scene2d


    【解决方案1】:

    我没有足够的代表发表评论,但我最好的猜测是您需要设置舞台视口。我还不能查看我的代码或 libgdx 源代码,但我会在回家时尝试查看。 Stage 有它自己的相机,所以我不确定它的默认构造函数是如何处理的。稍后会检查。

    【讨论】:

    • 感谢您的回答。实际上现在它起作用了。我所做的是创建一个全新的项目。并粘贴我以前的代码。现在它起作用了。这是非常罕见的事情发生。但是任何需要如何在 libgdx 中制作新按钮的人都可以使用上面的代码。
    【解决方案2】:

    我已经尝试了 stage.draw() 和批量绘制的东西,发现对我来说,在 Android 开发中,以下顺序有效:

    @Override
        public void render(float delta) {
            Gdx.gl.glClearColor(0, 0, 0, 1);
            Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
    
    
    
            batch.begin();
            batch.draw(test, 300, 300); //It works
            font.draw(batch, "BLA BLA!", 50, 500); //It works
            batch.end();
    
            // Put stage.draw() after batch drawing!
            stage.act(delta);
            stage.draw();
    
            }
    

    【讨论】:

      猜你喜欢
      • 2018-04-16
      • 2011-09-07
      • 2017-09-16
      • 1970-01-01
      • 1970-01-01
      • 2022-08-17
      • 2017-01-17
      • 2017-09-10
      • 1970-01-01
      相关资源
      最近更新 更多