首页 文章

为什么SpriteBatch没有绘制任何东西(LibGdx)?

提问于
浏览
1

这是从第6天的飞扬鸟类娱乐教程 - http://www.kilobolt.com/day-6-adding-graphics---welcome-to-the-necropolis.html

这是我在游戏中用于纹理的图像文件 . 它是一个256px x 64px .png文件 .

enter image description here

这是我用来加载纹理的类和我希望SpriteBatch绘制的特定TextureRegion(texure的一部分) .

public class AssetLoader {
          public static Texture texture;
          public static TextureRegion bg;

          public static void load() {
                  texture = new Texture(Gdx.files.internal("data/texture.png"));
                  bg = new TextureRegion(texture, 0, 0, 136, 43);
          }

   }
      And I call AssertLoader.load(), along with setting up game screen from
     public class MyGdxGame extends Game{
          @Override
         public void create() {
                  AssetLoader.load();
                 setScreen(new GameScreen());
         }
      }

      And inside GameScreen.java
      public class GameScreen implements Screen {
           //delegate render task
           private GameRenderer renderer;
          public GameScreen() {
               float screenHeight = Gdx.graphics.getHeight();
               float screenWidth = Gdx.graphics.getWidth();
               float gameWidth = 136;
               float gameHeight = screenHeight / (screenWidth / gameWidth);
               renderer = new GameRenderer((int)gameHeight);
           }
       }
      And inside GameRederer, the class I delegate to render the game
      public class GameRenderer {
            private int gameHeight;
            private SpriteBatch batch;
            private OrthographicCamera cam;
            public GameRenderer(int gameHeight) {
                   this.gameHeight = gameHeight;
                  cam = new OrthographicCamera();
                   batch = new SpriteBatch();
                   batch.setProjectionMatrix(cam.combined);
                    cam.setToOrtho(true, 136, gameHeight);
            }
             public void render() {
                  Gdx.gl.glClearColor(0, 0, 0, 1);
                  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
                  batch.begin();
                  batch.disableBlending();
                  batch.draw(AssetLoader.bg, 0, (gameHeight/2) + 23, 136, 43);
                  batch.end()
             }
        }

enter image description here

当我运行桌面版游戏时,我得到的是上面显示的黑色屏幕(黑色,因为我用这些代码行将背景设置为黑色

Gdx.gl.glClearColor(0, 0, 0, 1);
  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

有谁知道为什么没有出现SpritchBatch绘图?我用这行代码提取了我想要的纹理的纹理部分(从0,0开始,宽度为136,高度为43) - 使用GIMP - 找出要剪切的部分 .

bg = new TextureRegion(texture, 0, 0, 136, 43);

我还放了几个日志语句(从视图中删除它们)以确保在绘制之前,bg的宽度和高度设置正确,它们是 . 并且问题不能是游戏高度,因为我使用了print语句并且发现它是204,这意味着这个表达式(gameHeight / 2)23将评估为125,其在0和游戏高度之间的界限内 .

我检查了其他线程 . 我的问题不能是libgdx spritebatch not rendering textures因为SpriteBatch应该覆盖背景 . 它不能LibGDX: Android SpriteBatch not drawing因为我在桌面上运行我的,而不是andorid .

2 回答

  • 0

    如果有人有类似的问题,我解决问题的方法是打电话

    batch.setProjectionMatrix(cam.combined);
    

    cam.setToOrtho(true, 136, gameHeight);
    

    这对我来说真的没有意义,因为它在Camera.java中仍然是相同的Matrix4,就是这样

    public final Matrix4 combined = new Matrix4();
    

    希望其他人可以澄清这一点 .

  • 2

    可能是你必须先批量 cam.setToOrtho(true, 136, gameHeight); ,所以我无法确认希望有所帮助

    public GameRenderer(int gameHeight) {
                       this.gameHeight = gameHeight;
                      cam = new OrthographicCamera();
                       batch = new SpriteBatch();
                       cam.setToOrtho(true, 136, gameHeight);
                       batch.setProjectionMatrix(cam.combined);
    
                }
    

相关问题