LibGDX very strange bug - objects are disappeared

前端 未结 1 1805
小鲜肉
小鲜肉 2021-01-24 10:44

When I was creating my first tiled map creator in libGDX, I noticed a very strange bug. I creating grid of objects like this:

private static final i         


        
相关标签:
1条回答
  • 2021-01-24 10:56

    The camera is correct. The problem is the batch.begin() and batch.end(). As you might know you cannot do batch.begin() and then shaperenderer.begin() directly after each others without closing one of them. Reason for this I am not 100% about. stage works similar. This means we have to close the batch before drawing the stage

    batch.end();
    stage.draw();
    batch.begin();
    // draw your batch stuff here
    

    Also it's terrible to do this

    batch = new SpriteBatch();
    camera=new OrthographicCamera(CAM_WIDTH,CAM_HEIGHT);
    

    inside the render method. Instead, put it into the create() method or some of your own initialize method. The important thing is to not create a new SpriteBatch every frame as the batch isn't collected by the GC. So you have to manually dispose it using batch.dispose() or it will leak so much memory your RAM will be gone in no time.

    I hope this helped you out, good luck.

    0 讨论(0)
提交回复
热议问题