How to add Actor to a Screen with Libgdx?

1

I am not able to add actors (Actor) on a Screen. I want to show an actor in "ScreenMenu" and nothing happens (the actor does not appear), follow me code:

MainClass.java

public class MainClass extends Game{

public static Stage stage;
private OrthographicCamera gameCam;
private Viewport gamePort;
public static int V_WIDTH = 0;
public static int V_HEIGHT =0;
public SpriteBatch batch;

@Override
public void create() {

    V_WIDTH = Gdx.graphics.getWidth();
    V_HEIGHT = Gdx.graphics.getHeight();
    gameCam = new OrthographicCamera();
    gamePort = new FitViewport(V_WIDTH,V_HEIGHT, gameCam);
    batch = new SpriteBatch();
    stage = new Stage();

   setScreen(new ScreenMenu(this));

}

        @Override
public void dispose() {

    super.dispose();
    }

@Override
public void render() {
    super.render();
    Gdx.gl.glClearColor(color.r, color.g, color.b, color.a);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    stage.draw();

 }

ScreenMenu.java

public class ScreenMenu implements Screen {

public static Stage stage;
private Viewport viewport;
public SpriteBatch batch;
BotaoTrovaoActor btn;

private MainClass mainClass;

public ScreenMenu(MainClass mc)
{

    mainClass=mc;

}

@Override
public void show() {
    stage = new Stage();
    batch = new SpriteBatch();
    btn = new BotaoTrovaoActor();
    btn.setBounds(100,400 , 72, 72);
    stage.addActor(btn);


}


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

    stage.draw();


}

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

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {

}

}

    
asked by anonymous 01.05.2016 / 01:52

1 answer

2

Maybe it's because you're not setting the Stage viewport, the viewport is used to determine how the Stage will be displayed on the screen, I do not see you doing it in your code, you can see how to do it here:

private Stage stage;

public void create () {
    stage = new Stage(new ScreenViewport());
    Gdx.input.setInputProcessor(stage);
}

public void resize (int width, int height) {
    // Passing true when updating the viewport changes the camera position so it is centered on the stage, making 0,0 the bottom left corner
    stage.getViewport().update(width, height, true);
}

public void render (float delta) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(delta);
    stage.draw();
}

public void dispose() {
    stage.dispose();
}

If this is not the problem check if you have defined the draw method of your Actor

    
01.05.2016 / 21:10