简体   繁体   中英

LibGDX actor doesn't show

I have a problem with buttons and labels. I have following code in Create method:

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


    stage = new Stage(new ScreenViewport());
    atlas = new TextureAtlas("buttons/btnplay.pack");
    font = new BitmapFont();

    skin = new Skin(atlas);

    Table table = new Table(skin);
    table.setBounds(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    TextButtonStyle style = new TextButtonStyle();
    style.up = skin.getDrawable("btndown");
    style.down = skin.getDrawable("btndown");
    style.font = font;

    TextButton btnPlay = new TextButton("DELAASDASDSAD",style);
    btnPlay.setPosition(100, 100);
    btnPlay.setWidth(100);
    btnPlay.setHeight(100);

    btnPlay.background(skin.getDrawable("btnup"));

    table.add(btnPlay);

    stage.addActor(btnPlay);

And in rendering method:

stage.act(Gdx.graphics.getDeltaTime());
stage.draw();

Now the problem is that button or label doesn't show on screen and i don't see reason why. Any suggestion?

Add the table to the stage instead of the button and it should be fine. There is no need to add the button to the stage since you add the whole table. (regulary)

stage.addActor(btnPlay); 

To

stage.addActor(table);

Ok I found solution , in resize method i forget to update viewport.

so i just add this code :

stage.getViewport().update(width, height, true);

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