2
votes

In scene2d, the method stage.draw() is supposed to fire the draw method of all actors, but in my case it not fire even a single one. This is my code:

Setup:

public IntroScreen(DirectedGame game)
{
    super(game);

    batch = new SpriteBatch();
    camera = new OrthographicCamera(Constants.EDITOR_GUI_WIDTH, Constants.EDITOR_GUI_HEIGHT);
    camera.position.set(0, 0, 0);
    camera.setToOrtho(false);
    camera.update();

    stage = new Stage();
    achisoft = new Text("AchiSoft");
    achisoft.setVisible(true);
    achisoft.size(200);
    achisoft.setPosition(50, 50);
    stage.addActor(achisoft);
    Gdx.app.debug("stage","num_actors="+stage.getActors().size);

}

Render method:

public void show()
{
    stage = new Stage();
}

@Override
public void render(float deltaTime)
{
    getInputProcessor();
    Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
    stage.act(deltaTime);
    stage.draw();
    //Gdx.app.debug("render","deltaTime="+deltaTime);
}

Actor:

public class Text extends Actor
{
    BitmapFont fontt;
    String text;
    Texture textur;

    public Text(String cad)
    {
        text=cad;
        fontt=Assets.instance.fonts.defaultBig;
        Vector2 res = new Vector2();
    res.set(512, 512);
    Pixmap pixmap = new Pixmap(512, 512, Format.RGBA8888);
    pixmap.setColor( 0, 1, 0, 1f );
    pixmap.fillRectangle(1,1,256,256);
    pixmap.setColor( 1, 0, 0, 1f );
    pixmap.fillRectangle(257,257,256,256);
    textur = new Texture(pixmap);
    pixmap.dispose();
    Gdx.app.debug("texture","entra");
    }

    @Override
    public void draw(Batch batch, float parentAlpha)
    {
        fontt.setColor(1, 0, 0, 1); // red
    fontt.draw(batch, text, 20, 20);
    batch.draw(textur,50,50);
    Gdx.app.debug("texture","se pinta");
    }

    @Override
    public Actor hit(float x, float y, boolean touchable)
    {
        // TODO Auto-generated method stub
        return super.hit(x, y, touchable);
    }
}

Resize:

@Override
public void resize(int width, int height)
{
    stage.setViewport(width, height, false);
    Gdx.app.debug("size",width+ " "+height);
    camera.setToOrtho(false, width, height);
    batch.setProjectionMatrix(camera.combined);
}

The debug string in actor's constructor is printed, but the debug string in the actor's draw is not printer. I have test to draw in the render a texture with batch.draw, and works fine. But the actor is never render.

1
Does act get called? If yes, it is possible, that your Actor is out of the Stages Viewport and so it gets culled, meaning draw() will never be called.Robert P
I think the actor is inside the viewport, the debut print of the viewport of the stage in each render stage: x=1920.0 y=1080.0Raúl Ramírez
Do you use the camera and the spritebatch you create in the IntroScreen constructor?Robert P
I put also the resize() function. This is almost the whole code of that screen. I only use what you see. I have used scene2d objects in different manner successfully in other screens, but in IntroScreen, a simple draw such as this is not working. I am wasted so time in surely a tiny bug or typo that i can't see...Raúl Ramírez
Just a thing to notice: Stage has its own camera and SpriteBatch. So the SpriteBatch you set up in the constructor and in the resize is never used. You need to call stage.setSpriteBatch so that stage uses your SpriteBatchRobert P

1 Answers

1
votes

The problem was in the show() method, that reinitialize the stage. The show() are invoked after the constructor, so the stage doesn't have any of the stuff declared in the constructor. Comment the line, solved the problem.

public void show()
{
    //stage = new Stage();
}

Also, i have set the camera to the camera of the stage to the main camera, as Springrbua said.

stage.setCamera(camera);