Libgdx Rendering Textures vs Sprites

I am trying to create my first game with Libgdx and Box2d. The game is similar to the Flappy Bird concept. My problem is pipe rendering.

I tried drawing rectangles and then drawing a new sprite, which I can resize to different pipe sizes every time the render method is called. the problem is that I cannot get rid of the texture when the rectangle leaves the screen because it will cause all the other rectangles that are still visible to lose their texture. if I don’t get rid of the texture when it leaves the screen, this will make the game very slow after 20 seconds.

Another option is to use about 10 different textures for different pipe sizes, but there is still the problem of removing textures.

I would appreciate any advice on efficiently displaying different pipe sizes. I added the render code below

  @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);
}
+4
source share
1 answer

You must support the render () method as quickly as possible. Resource loading is quite slow, so you must create your textures and sprites in the create () method.

In addition, you do not need as many sprites as raindrops, you can reuse the same sprite, change its position and size and draw it many times.

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1527539/


All Articles