-2

我正在使用 JAVA 和 OpenGL-ES 在 Android 中开发 2D 视频游戏。

我有一个问题,我认为是垃圾收集器。每隔几秒钟,游戏就会冻结,无论我在做什么。

我一直在阅读一些关于它的文档等等,我删除了我拥有的所有循环迭代器,现在我使用 for(i=0,...) 以及其他解决方案。情况是,它对性能没有任何影响......

我一直在查看我的代码,发现了一些我认为可能会造成问题的东西,这就是我在动画中的精灵之间进行更改的方式。

例如,我有一个可以按一些键移动的英雄。当它行走时,他的精灵在帧之间变化。为此,我移动纹理缓冲区以瞄准我想要的图像部分。每次这样做时,我都会使用此功能:

protected void SetTextureBuffer(float xo, float xf, float yo, float yf) {

    float textureVertexs[] = {
            xo, yf,     // top left
            xf, yf,     // bottom left
            xf, yo,     // top right
            xo, yo      // bottom right
    };

    // a float has 4 bytes so we allocate for each coordinate 4 bytes
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(textureVertexs.length * 4);
    byteBuffer.order(ByteOrder.nativeOrder());

    // allocates the memory from the byte buffer
    textureBuffer = byteBuffer.asFloatBuffer();

    // fill the vertexBuffer with the vertices
    textureBuffer.put(textureVertexs);

    // set the cursor position to the beginning of the buffer
    textureBuffer.position(0);
}

每次调用它都会分配内存来创建缓冲区,这可能是每秒很多次,因为有更多的带有动画的实体......

你认为这可能是个问题吗?还是我看错了?如果这是一个问题......我怎样才能以另一种更有效的方式解决这个问题?

4

1 回答 1

1

尝试删除这些行:

ByteBuffer byteBuffer = ByteBuffer.allocateDirect(textureVertexs.length * 4);
byteBuffer.order(ByteOrder.nativeOrder());
textureBuffer = byteBuffer.asFloatBuffer();

textureBuffer在您的类构造函数/初始化程序中分配一次。由于您不需要保留以前的数据,因此无需再次重新创建缓冲区。因此,您只需覆盖同一缓冲区中的旧数据。

于 2013-01-24T16:39:58.450 回答