【发布时间】:2019-05-23 05:35:00
【问题描述】:
我是 libgdx 的新手,一直在练习使用 ShapeRenderer。我试图在屏幕底部放置一个矩形,但 Gdx.graphics.getHeight() 函数返回的值未覆盖整个长度。如果我将矩形的坐标设置为(0, screenHeight),它只会出现在屏幕的中间。
另外,当我记录来自 getWidth() 和 getHeight() 函数的维度值时,它们都返回 1440。
This is the rectangle (White section) 当width = getWidth() 和height = getHeight()。
代码:
public class GameClass extends ApplicationAdapter {
private SpriteBatch batch;
private Texture goalTexture;
private Sprite sprite;
private OrthographicCamera cam;
private ShapeRenderer shape;
private ScreenViewport viewport;
//x-axis length for top/bottom bar
private float goalWidth = 200;
//y-axis height for back bar
private float goalHeight;
private float goalPostThickness = 20;
//Screen height and width
private float screenWidth;
private float screenHeight;
//How far down/up posts are from edge of screen
private float goalPostOffset;
@Override
public void create() {
viewport = new ScreenViewport();
cam = new OrthographicCamera();
cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
shape = new ShapeRenderer();
}
@Override
public void dispose() {
batch.dispose();
goalTexture.dispose();
shape.dispose();
}
@Override
public void render() {
//Logic
screenWidth = Gdx.graphics.getWidth();
screenHeight = Gdx.graphics.getWidth();
System.out.println(screenWidth);
System.out.println(screenHeight);
goalPostOffset = screenHeight/3;
//Draw
cam.update();
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
shape.setProjectionMatrix(cam.combined);
//Top goal bar
shape.setColor(Color.WHITE);
shape.begin(ShapeRenderer.ShapeType.Filled);
shape.rect(0, 0, screenWidth, screenHeight);
shape.end();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
}
【问题讨论】:
标签: java android android-studio libgdx