android – Libgdx渲染纹理与精灵

我正在尝试使用Libgdx和Box2d构建我的第一个游戏.

游戏与Flappy Bird有类似的概念.我的问题是渲染管道.

我已经尝试绘制矩形然后绘制新的精灵,每次调用渲染方法时我都可以缩小到不同的管道大小.这样做的问题是,一旦矩形离开屏幕,我就无法处理纹理,因为它会使所有其他仍然可见的矩形失去纹理.如果我离开屏幕时没有处理纹理,它会在20秒后使游戏变得非常慢.

另一种选择是针对不同的管道尺寸使用大约10种不同的纹理,但仍然存在处理纹理的问题.

如果有效地渲染不同的管道尺寸,我将不胜感激.我在下面附上了渲染代码

  @Override
public void render(float delta) {
    Gdx.gl.glClearColor(0,0,0,0);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);

    batch.setProjectionMatrix(camera.combined);

    batch.begin();
    //background.setPosition(camera.position.x-camera.viewportWidth/2, camera.position.y-14);
    //background.draw(batch);
    batch.end();

    //bg1.update();
    //bg2.update();

    updateAnimation();

    if((TimeUtils.nanoTime()/10) - (lastDropTime/10) > 300000000) 
        createPipes();

       batch.begin();

       for(Rectangle raindrop: raindrops) {
              pipe_top.setSize(4, raindrop.height);
          pipe_top.setPosition(raindrop.x,  raindrop.y);
          pipe_top.draw(batch);

          pipe_bottom.setSize(4, raindrop.height);
          pipe_bottom.setPosition(raindrop.x, camera.position.y + (camera.viewportHeight/2-pipe_bottom.getHeight()));
          pipe_bottom.draw(batch);

       }
       batch.end();

       if(pipe.getX() < 0){
           pipe.getTexture().dispose();
       }

       Iterator<Rectangle> iter = raindrops.iterator();
          while(iter.hasNext()) {
             Rectangle raindrop = iter.next();
             raindrop.x -= 4 * Gdx.graphics.getDeltaTime();

             if(raindrop.x  < -35) iter.remove();

          }

    debug.render(world, camera.combined);
}

最佳答案 您必须尽快维护render()方法.加载资源非常慢,因此您应该在create()方法中创建纹理和精灵.

此外,你不需要像雨滴一样多的精灵,你可以重复使用相同的精灵,改变它的位置和大小,并绘制多倍的时间.

希望这可以帮助.

点赞