2

在scene2d 中,stage.draw() 方法应该触发所有actor 的draw 方法,但在我的情况下,它甚至不会触发一个。这是我的代码:

设置:

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

}

渲染方法:

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);
}

演员:

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);
    }
}

调整大小:

@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);
}

打印了actor构造函数中的调试字符串,但是actor的draw中的调试字符串不是打印机。我已经测试用batch.draw在渲染中绘制纹理,并且工作正常。但是演员永远不会渲染。

4

1 回答 1

1

问题出在 show() 方法中,该方法重新初始化了舞台。show() 在构造函数之后被调用,所以舞台没有在构造函数中声明的任何东西。注释行,解决问题。

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

另外,正如Springrbua所说,我已将相机设置为舞台相机为主相机。

stage.setCamera(camera);
于 2014-04-17T16:26:52.660 回答