gpt4 book ai didi

java - libgdx getHeight() 不覆盖整个屏幕

转载 作者:行者123 更新时间:2023-11-29 04:13:07 28 4
gpt4 key购买 nike

我是 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() {
}
}

最佳答案

您发布的代码部分有错别字。您在高度中加载宽度screenHeight = Gdx.graphics.getWidth();

关于java - libgdx getHeight() 不覆盖整个屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53906920/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com