libgdx getHeight() 没有覆盖整个屏幕

Posted

技术标签:

【中文标题】libgdx getHeight() 没有覆盖整个屏幕【英文标题】:libgdx getHeight() does not cover the whole screen 【发布时间】: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() 
    

【问题讨论】:

【参考方案1】:

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

【讨论】:

我是个白痴。你是一个救生员,我整天都在为此苦苦挣扎。谢谢!

以上是关于libgdx getHeight() 没有覆盖整个屏幕的主要内容,如果未能解决你的问题,请参考以下文章

Libgdx |将黑色设置为圆圈外的所有内容

无法让事件在我的 libgdx Actor 中工作

应用程序全屏后,Libgdx控制器无响应

LibGDX:如何从主游戏屏幕重复显示/隐藏覆盖弹出菜单

影响整个屏幕的Libgdx着色器

Monotouch对话框多行元素高度没有增长