Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
186 views
in Technique[技术] by (71.8m points)

java - FitViewport does not scale actor

I have FitViewport with virtual screen size 1920x1080.

viewport = new FitViewport(AssetLoader.VIRTUAL_WIDTH, AssetLoader.VIRTUAL_HEIGHT);
stage = new Stage(viewport);

Also, there is TimeBar class extended from Actor class. TimeBar listing:

public class TimeBar extends Actor {
private Color color;
private ShapeRenderer renderer;
private float time, current;

public TimeBar() {
    color = Color.valueOf(AssetLoader.TIMEBAR_COLOR);
    renderer = new ShapeRenderer();
    renderer.setColor(color.r, color.g, color.b, color.a);

    setBounds(10, 10, 1800, 10); //rectangle is wider than the screen! Why?
}

@Override
public void draw(Batch batch, float parentAlpha) {
    super.draw(batch, parentAlpha);
    batch.end();
    renderer.begin(ShapeRenderer.ShapeType.Line);
    renderer.rect(getX(), getY(), getWidth(), getHeight());
    renderer.end();
    batch.begin();
}
}

I've added some logs to render method:

    Gdx.app.log("GameRenderer", "resizing: " + width + " " + height);
    Gdx.app.log("GameRenderer", "world width: " + viewport.getWorldWidth());
    Gdx.app.log("GameRenderer", "stage width: " + stage.getWidth());
    Gdx.app.log("GameRenderer", "Time bar width: " + timeBar.getWidth());

They said:

GameRenderer: world width: 1920.0
GameRenderer: stage width: 1920.0
GameRenderer: Time bar width: 1800.0

But rectangle is wider then the screen, why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're creating Stage with predefined Viewport, so projection matrix of stage's batch is defined by stage viewport but when you create ShapeRenderer then projection matrix is defined by Gdx.graphics.getWidth() and by Gdx.graphics.getHeight()

write these two lines in draw() method.

renderer.setProjectionMatrix(batch.getProjectionMatrix());
renderer.setTransformMatrix(batch.getTransformMatrix());

Or you can use this class

https://github.com/itsabhiaryan/gdx-utils/blob/master/gdx-utils/src/com/ng/gdxutils/actor/ShapeRendererActor.java


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...