LibGDX非常奇怪的bug - 对象消失了

时间:2016-12-31 11:32:17

标签: object libgdx

当我在libGDX创建我的第一个平铺地图创建者时,我发现了一个非常奇怪的错误。我创建这样的对象网格:

private static final int GRID_WIDTH=2400;
private static final int GRID_HEIGHT=2400;
private static final int CELL_SIZE=60;

所以你可以看到有2400 / 60x2400 / 60个对象或单元格。我正在创建这样的地图:

private void createMap(){
    cells = new Cell[GRID_WIDTH/CELL_SIZE][GRID_HEIGHT/CELL_SIZE];

    for(int i=0;i<GRID_WIDTH/CELL_SIZE;++i){
        for(int j=0;j<GRID_HEIGHT/CELL_SIZE;++j){
            cells[i][j]=new Cell(textures[0],i*CELL_SIZE,j*CELL_SIZE);
        }
    }
}

我在屏幕上也有调试坐标,所以我知道它们开始消失的位置。 Y坐标确定从0到2400,但在X上它们开始在1500处消失。当我开始绘制时,每个列的一些纹理将对该纹理可见(例如,当我开始在x =处写纹理时) 2100每个消失的列都将在2100年可见,当我删除该纹理时,每列将再次消失到1500.所以对象在那里,但它们不可见。有人知道这个bug真烦人吗?

正如您所见,坐标位于左下方,这是在开头:

enter image description here

这是我在那里添加一些纹理

enter image description here

[已编辑]带相机的代码:

        private float x=GRID_WIDTH/2,y=GRID_HEIGHT/2;

        @Override
        public void render(float delta) {
            batch = new SpriteBatch();
            camera=new OrthographicCamera(CAM_WIDTH,CAM_HEIGHT);
            viewPos = new Vector3();

            Gdx.gl.glClearColor(0, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            viewPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
            camera.unproject(viewPos);

            batch.begin();

            if(Gdx.input.isKeyPressed(Input.Keys.RIGHT) || Gdx.input.isKeyPressed(Input.Keys.D))
                x+=SPEED*Gdx.graphics.getDeltaTime();
            if(Gdx.input.isKeyPressed(Input.Keys.LEFT) || Gdx.input.isKeyPressed(Input.Keys.A))
                x-=SPEED*Gdx.graphics.getDeltaTime();
            if(Gdx.input.isKeyPressed(Input.Keys.UP) || Gdx.input.isKeyPressed(Input.Keys.W))
                y+=SPEED*Gdx.graphics.getDeltaTime();
            if(Gdx.input.isKeyPressed(Input.Keys.DOWN) || Gdx.input.isKeyPressed(Input.Keys.S))
                y-=SPEED*Gdx.graphics.getDeltaTime();
            stage.act(Gdx.graphics.getDeltaTime());
            stage.draw();

            camera.position.set(x,y,0);
            camera.update();
            batch.setProjectionMatrix(camera.combined);
            batch.end();
      }

1 个答案:

答案 0 :(得分:2)

相机是正确的。问题是batch.begin()batch.end()。您可能知道,在不关闭其中一个之后,您无法直接执行batch.begin()然后shaperenderer.begin()。原因我不是100%左右。 stage的工作方式类似。这意味着我们必须在绘制阶段之前关闭批处理

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

这样做很糟糕

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

在render方法中。相反,将它放入create()方法或某些自己的初始化方法中。重要的是不要每帧都创建一个新的SpriteBatch,因为GC没有收集批次。因此,您必须使用batch.dispose()手动处理它,否则会泄漏太多内存,您的RAM将立即消失。

我希望这能帮到你,祝你好运。