LibGDX 非常奇怪的 bug - 对象消失了 [英] LibGDX very strange bug - objects are disappeared

查看:21
本文介绍了LibGDX 非常奇怪的 bug - 对象消失了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 libGDX 中创建我的第一个平铺地图创建器时,我注意到一个非常奇怪的错误.我创建这样的对象网格:

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 int GRID_WIDTH=2400;
private static final int GRID_HEIGHT=2400;
private static final int CELL_SIZE=60;

因此您可以看到有 2400/60x2400/60 个对象或单元格.我正在像这样创建我的地图:

so you can see there are 2400/60x2400/60 objects or cells. I am creating my map like this:

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吗?

I also have coordinates for my debug in the screen so I know where they started to disappear. Y coordinate is ok there are from 0 to 2400, but on the X they started to disappear at 1500. When I start to draw there some texture every column will be visible to that texture for example (when I start to write texture at x=2100 every disappeared column will be visible to 2100) and when I will delete that texture every column will disappear again to 1500. So the objects are there but they are not visible. It is so annoying does anyone know about this bug?

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

As you can see coordinates are at the bottom left this is at the beginning:

这是我将在那里添加一些纹理的时候

and this is when I will add there some texture

[已编辑] 带摄像头的代码:

Code with camera:

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

推荐答案

摄像头是正确的.问题在于 batch.begin()batch.end().您可能知道,您不能在不关闭其中一个的情况下直接执行 batch.begin()shaperenderer.begin().我不是100%的原因.stage 的工作方式类似.这意味着我们必须在绘制阶段之前关闭批处理

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

这样做也很糟糕

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

在渲染方法中.相反,将其放入 create() 方法或您自己的一些初始化方法中.重要的是不要每帧都创建一个新的 SpriteBatch,因为 GC 不会收集批处理.所以你必须使用 batch.dispose() 手动处理它,否则它会泄漏这么多内存,你的 RAM 很快就会消失.

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.

这篇关于LibGDX 非常奇怪的 bug - 对象消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆