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

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

问题描述

当我在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.因此对象存在,但它们不可见.有谁知道这个错误,这真让人讨厌吗?

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

在render方法内部.而是将其放入create()方法或您自己的一些initialize方法中.重要的是不要在每个帧上创建一个新的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非常奇怪的错误-对象消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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