代码之家  ›  专栏  ›  技术社区  ›  burprinz

纹理未渲染的顶部和右侧的LibGdx黑框

  •  0
  • burprinz  · 技术社区  · 2 年前

    我刚开始用LibGdx创建一个游戏,但当我在窗口的顶部或右侧渲染纹理时,它不会显示在屏幕上。就像在纹理上画了两个黑框。 ( https://i.sstatic.net/goVAZ.png )

    这是代码:

    public class SpaceInvaders extends ApplicationAdapter {
    
        SpriteBatch batch;
        //MainUI mUI;
    
        Player p;
    
        @Override
        public void create () {
            batch = new SpriteBatch();
            Gdx.graphics.setResizable(false);
            Gdx.graphics.setWindowedMode(672, 768);
            //mUI = new MainUI(batch);
            p = new Player(batch);
        }
    
        @Override
        public void render () {
    
            // Update
            //mUI.update();
            p.update();
    
            // Render
            batch.begin();
            ScreenUtils.clear(0, 0, 0, 1);
    
            //mUI.render();
            p.render();
    
            batch.end();
        }
    
        @Override
        public void dispose () {
            batch.dispose();
        }
    }
    
    public class Player {
    
        SpriteBatch batch;
        Texture playerImg;
        Sprite sprite;
        //Projectile p;
    
        public int x = 0, y = Gdx.graphics.getHeight() - 300f;
    
        public Player(SpriteBatch batch) {
            this.batch = batch;
            playerImg = new Texture("res/player.png");
            sprite = new Sprite(playerImg);
        }
    
        public void update() {
            updateKeys();
            /*
            if(p != null) {
                p.update();
                if(p.y+p.image.getHeight() > Gdx.graphics.getHeight()) {
                    p = null;
                }
            }
             */
        }
    
        private void updateKeys() {
            // D
            if(Gdx.input.isKeyPressed(32)) {
                x+=3;
                if(x+playerImg.getWidth() > Gdx.graphics.getWidth()) x = Gdx.graphics.getWidth()-playerImg.getWidth();
            }
            // A
            if(Gdx.input.isKeyPressed(29)) {
                x-=3;
                if(x < 0) x = 0;
            }
            // Space
            /*
            if(Gdx.input.isKeyPressed(62)) {
                if(p == null) {
                    p =new Projectile(batch, x+playerImg.getWidth()/2-2, y+playerImg.getHeight()+1, 5);
                }
            }
             */
            sprite.setX((float) x);
            sprite.setY(Gdx.graphics.getHeight() - 300f);
        }
    
        public void render() {
            sprite.draw(batch);
            /*
            if(p != null) {
                p.render();
            }
             */
        }
    
    }
    

    我试着移除除了玩家之外的所有东西,但仍然不起作用。

    0 回复  |  直到 2 年前
        1
  •  0
  •   isaid    2 年前

    我测试了你的代码,你是对的。我也是初学者。解决方法是删除您的:

    Gdx.graphics.setResizable(false);
    Gdx.graphics.setWindowedMode(672, 768);
    

    转到您的桌面>src>com.mygdxgame>DesktopLauncher并添加此:

    config.setResizable(false);
    config.setWindowedMode(672, 768);