libgdx ProjectionMatrix用于舞台?

时间:2018-10-18 08:41:27

标签: android libgdx stage projection-matrix

嘿,我的ProjectionMatrix有问题。我在与标签相同的x坐标处绘制了一个矩形,但它们不在同一位置。舞台上有像ProjectionMatrix这样的东西吗?

@Override
public void create() {
    batch = new SpriteBatch();
    stage = new Stage();
    shapeRenderer = new ShapeRenderer();
    camera = new OrthographicCamera();
    camera.setToOrtho(false, 480, 800);

    //create label
    generator = new FreeTypeFontGenerator(Gdx.files.internal("arial-black.ttf"));
    parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
    parameter.size = 50;
    parameter.color = Color.BLACK;
    font = generator.generateFont(parameter);
    labelStyle = new Label.LabelStyle();
    labelStyle.font = font;
    label = new Label("text", labelStyle);
    label.setPosition(240, 400);
    stage.addActor(label);
}

@Override
public void render() {
    delta = Gdx.graphics.getDeltaTime();
    Gdx.gl.glClearColor(255, 255, 255, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    // tell the camera to update its matrices.
    camera.update();

    // tell the SpriteBatch to render in the
    // coordinate system specified by the camera.
    batch.setProjectionMatrix(camera.combined);
    shapeRenderer.setProjectionMatrix(batch.getProjectionMatrix());

    stage.act(delta);
    stage.draw();

    shapeRenderer.begin(ShapeRenderer.ShapeType.Filled);
    shapeRenderer.setColor(Color.BLACK);
    shapeRenderer.rect(label.getX(), 0, 50, 50);
    shapeRenderer.end();
  }

1 个答案:

答案 0 :(得分:1)

将舞台构造函数与视口一起使用:

/** Creates a stage with the specified viewport. The stage will use its own {@link Batch} which will be disposed when the stage
 * is disposed. */
public Stage (Viewport viewport) {
    this(viewport, new SpriteBatch());
    ownsBatch = true;
}

Viewport viewport = new FitViewport(800, 480, camera);
Stage stage = new Stage(viewport);