How to add Actor on a Screen with Libgdx?

Asked

Viewed 150 times

1

I’m not able to add actors (Actor) on a screen.I want to show an actor in the "Screenmenu" and nothing happens (the actor does not appear), follows 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() {

}

}

1 answer

2


Maybe it’s because Voce is not setting the Stage viewport, the viewport is used to determine how the Stage will be shown on the screen, I don’t see Voce doing this in your code, Voce 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 checks if Voce defined your actor’s draw method

  • 2

    It would be interesting to contextualize the content (or code) of the link, responses only with links are discouraged here, see more on We want answers that contain only links?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.