FitViewport不会缩放actor

时间:2017-03-05 20:31:04

标签: java libgdx

我的{% extends '_base.html' %} {% block content %} <form class="logVacation" enctype=multipart/form-data role="form" method="post" action="/post"> {{ form.csrf_token }} {{ form.vacation_name(placeholder="Name Your Vacation")}} <br> {{ form.location(placeholder="Where was it?") }} <br> {{ form.with_who(placeholder='Who was with you') }} <br><br> {{ form.description(placeholder="Tell us about your vacation... or not.") }}<br> {{ form.when(class="datepicker", placeholder="when?") }} <br><br> {{ form.photo(multiple="multiple") }} <br> <button class="btn btn-sm btn-success" value="upload" type="submit">Done</button> </form> {% endblock %} 虚拟屏幕尺寸为1920x1080。

FitViewport

此外,viewport = new FitViewport(AssetLoader.VIRTUAL_WIDTH, AssetLoader.VIRTUAL_HEIGHT); stage = new Stage(viewport); 类扩展了TimeBar个类。 Actor列出:

TimeBar

我已将一些日志添加到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(); } } 方法:

render
他们说:

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

但矩形比屏幕宽,为什么?

1 个答案:

答案 0 :(得分:1)

您正在使用预定义的视口创建舞台,因此舞台批处理的投影矩阵由舞台视口定义,但是当您创建ShapeRenderer时,投影矩阵由Gdx.graphics.getWidth()Gdx.graphics.getHeight()

定义

draw()方法中写下这两行。

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

或者您可以使用此课程

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