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

时间:2014-12-13 09:28:12

标签: java android libgdx sprite spritebatch

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

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

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 :(得分: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);

            }

答案 1 :(得分:0)

如果有人遇到类似的问题,我解决问题的方法是致电

        batch.setProjectionMatrix(cam.combined);

之后

   cam.setToOrtho(true, 136, gameHeight);

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

    public final Matrix4 combined = new Matrix4();

希望其他人能够澄清这一点。

相关问题