2014-12-13 112 views
1

这是从笨鸟先飞娱乐教程 的第6天 - http://www.kilobolt.com/day-6-adding-graphics---welcome-to-the-necropolis.html为什么不SpriteBatch绘制任何东西(LibGdx)?

这里是图像文件我使用纹理在我的游戏。它是一个256像素x 64像素的.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的宽度和高度被正确设置,它们是。而且这个问题不能成为游戏高度,因为我使用了打印语句,并且发现它是204,这意味着这个表达式(gameHeight/2)+ 23将评估为125,这是在0和游戏高度之间的界限。

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

+0

好吧,让我给你一些一般性的建议。我建议你阅读“学习libGDX游戏开发”。这本书适合初学者,它会给你关于libgdx的基本知识。如果你没有钱,你知道该怎么做,对吧?你是否也尝试从该教程下载源代码?也许你错过了什么。这是我的源代码https://github.com/nikoliazekter/JustSimpleTD。它可以帮助你。 – nikoliazekter 2014-12-13 10:39:36

+0

你在哪里打电话给gameRenderer.render()? – grimrader22 2014-12-13 15:20:27

+0

grimdradar22,我知道它被调用,因为背景是我指定的黑色背景。 – committedandroider 2014-12-13 15:51:39

回答

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); 

      } 
+0

这就是问题:)你知道为什么?当你调用builder camera,然后调用setProjectionMatrix(cam.combined);;然后当你说cam.setToOrtho(true,136,gameHeight)时重新定义摄像机;当你调用摄像机时,@committedandroider和摄像机 – committedandroider 2014-12-13 16:39:22

+0

相同。分配一个矩阵,但在改变之后,希望它是,你的意思,我解释得很好 – 2014-12-13 16:48:14

0

如果任何人有类似的问题,我的方式解决了这个问题是调用

 batch.setProjectionMatrix(cam.combined); 

cam.setToOrtho(true, 136, gameHeight); 

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

public final Matrix4 combined = new Matrix4(); 

希望别人能澄清一点。

相关问题