简体   繁体   中英

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. You're calling it without closing the spritebatch before. If you look into stage's source code you'll see it has its own spritebatch. Call batch.end() before drawing the stage.

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) . 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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