简体   繁体   中英

Why doesn't SpriteBatch draw anything (LibGdx)?

This is from day 6 of the flappy bird recreation tutorial - http://www.kilobolt.com/day-6-adding-graphics---welcome-to-the-necropolis.html

Here is the image file i am using for texture in my game. It is a 256px x 64px .png file.

在此处输入图片说明

Here is the class that I used for loading the texture and the specific TextureRegion(part of the texure) that I want the SpriteBatch to draw.

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

在此处输入图片说明

What I get when I run the desktop version of the game is the black screen shown above(black because i set the background to black with these lines of code

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

Does anyone know why the SpritchBatch drawing isn't showing up? I extracted the texture portion of the texture I wanted with this line of code(starts from 0,0, width of 136, height of 43) - used GIMP - to find out portion to cut.

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

I also put a few log statements(removed them from view) to ensure that before drawing, bg's width and height were set correctly, which they were. And the issue can't be game height because I used a print statement and found that to be 204 which means that this expression, (gameHeight/2) + 23 will evaluate to 125 which is in bounds between 0 and game height.

I checked out other threads as well. My issue can't be libgdx spritebatch not rendering textures because the SpriteBatch should overwrite the background. And it can't be LibGDX: Android SpriteBatch not drawing because i am running mine on desktop, not andorid.

could be that you have to first put cam.setToOrtho(true, 136, gameHeight); before the batch, so I can not confirm hopefully help

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

            }

If anyone's having a similar issue, the way I solved the problem was to call

        batch.setProjectionMatrix(cam.combined);

after

   cam.setToOrtho(true, 136, gameHeight);

Which didn't really make sense to me because it's still the same Matrix4 in Camera.java, that is

    public final Matrix4 combined = new Matrix4();

Was hoping someone else could clarify that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM