简体   繁体   English

无法在LibGDX上同时绘制字体和舞台

[英]Unable to draw a font and a stage at the same time on LibGDX

I'm trying to draw a stage and a string with a specific font, but when I draw the stage, the string disappears, but the images do not. 我正在尝试绘制舞台和带有特定字体的字符串,但是当我绘制舞台时,字符串消失了,但是图像却没有。 I suppose it's a simple thing, but I have no idea. 我想这很简单,但是我不知道。 I've already tried to change the order to render, but that doesn't seem to be the problem. 我已经尝试过更改渲染顺序,但这似乎不是问题。 Any help would be aprecciated. 任何帮助将不胜感激。

@Override
public void create() {

    stage = new Stage();
    Gdx.input.setInputProcessor(stage);
    batch = new SpriteBatch();
    textatlas = new TextureAtlas("Agorafunfa.txt");
    TextureAtlas.AtlasRegion a = textatlas.findRegion("spider");
    spider = new Sprite(a);
    img = new Texture("Captura.PNG");
    yesa = new BitmapFont(Gdx.files.internal("yesa.fnt"));
    font = "Escape Planet";
    img3 = new Texture("saturno.png");
    funciona = new BitmapFont(Gdx.files.internal("yesa.fnt"));
    starte = "Começar";
    opcoes = "Opções";
    Skin skin = new Skin();
    skin.addRegions(ta);
    final TextButton.TextButtonStyle tbs = new TextButton.TextButtonStyle();
    tbs.font = yesa;
    tbs.checked = skin.getDrawable("comecaversao2");
    tbs.up = skin.getDrawable("comeca");
    b = new TextButton("Começar",tbs);
    b.setHeight(250);
    b.setWidth(300);
    b.setPosition(-10, 50);
    b.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
        }
    });
    stage.addActor(b);
    Gdx.input.setInputProcessor(stage);
}

public void render() {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(img,0,0);
    batch.draw(img3, 200,170, 250, 170);
    yesa.setColor(Color.WHITE);
    yesa.draw(batch,font,430,300);
    stage.draw();//if I comment this line, the string appears
    batch.end();
}

You're on right track, stage.draw() is causing the problem. stage.draw()正轨, stage.draw()导致了问题。 You're calling it without closing the spritebatch before. 您在调用它之前没有关闭spritebatch。 If you look into stage's source code you'll see it has its own spritebatch. 如果您查看Stage的源代码,您将看到它具有自己的spritebatch。 Call batch.end() before drawing the stage. 在绘制舞台之前,请调用batch.end()

Rendering a batch inside another opened batch will result in all kind of weird behaviour that makes you think the fault lies in everything but the batch. 在另一个打开的批处理中渲染一个批处理将导致各种奇怪的行为,使您认为故障不在于该批处理。


As these type of problems pop up frequently nowadays I checked out the stage docs , there is clear information that it uses its own batch, you can even set your own with the constructor stage(Viewport viewport, Batch batch) . 由于这些类型的问题如今经常出现,因此我检查了Stage文档 ,有明确的信息表明它使用自己的批处理,您甚至可以使用构造函数stage(Viewport viewport, Batch batch)来设置自己的stage(Viewport viewport, Batch batch) If you use that constructor you don't need to use the solution above. 如果使用该构造函数,则无需使用上述解决方案。 However the docs doesn't mention batches needs to be closed, not even in the batch docs . 但是,文档中没有提到需要关闭批次,甚至在批次文档中也没有 This is something that probably should be added so we can avoid the same issues popping up. 可能应该添加一些内容,这样我们可以避免出现同样的问题。

Possible duplicate of this and this . this的可能重复项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM