libgdx getHeight()不能覆盖整个屏幕

时间:2018-12-23 20:24:54

标签: java android android-studio libgdx

我是libgdx的新手,并且一直在练习使用ShapeRenderer。我试图在屏幕底部放置一个矩形,但是Gdx.graphics.getHeight()函数返回的值不能覆盖整个长度。如果我将矩形的坐标设置为(0, screenHeight),则它只会在屏幕上移一半。

此外,当我记录getWidth()getHeight()函数的尺寸值时,它们都返回1440。 width = getWidth()height = getHeight()时的This is the rectangle (White section)

代码:

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

1 个答案:

答案 0 :(得分:3)

您在代码部分张贴的是错字。您在高度中加载宽度 screenHeight = Gdx.graphics.getWidth();