How to put a TextureRegion [] in a TileMap in libGDX?

1

I am creating a game and for the character I used an array of TextureRegion, in order to animate the movement when walking. I have everything done, except that when drawing the character with the SpriteBatch . When I pass behind an object specified in a layer other than the TileMap , the character passes in front.

You will understand it with this image:

Would anyone know how to handle it? Or rather, instead of using a TextureRegion[] using a Sprite and, if this is the case, how to animate said Sprite with TextureRegion[] ? Thank you!

EDIT:

I add here the piece of code that has not yet been completely finished, it still needs to be tweaked, but I mainly want to make it work to focus on logic and bugs.

CHARACTER CLASS:

public TextureRegion[] getPersonaje(String personaje, int direccion) {
    Texture p = new Texture(personaje + ".png");

    region[0] = new TextureRegion(p, 0, ALTO_PERSONAJE * direccion, ANCHO_PERSONAJE, ALTO_PERSONAJE);
    region[1] = new TextureRegion(p, ANCHO_PERSONAJE, ALTO_PERSONAJE * direccion, ANCHO_PERSONAJE, ALTO_PERSONAJE);
    region[2] = new TextureRegion(p, ANCHO_PERSONAJE * 2, ALTO_PERSONAJE * direccion, ANCHO_PERSONAJE, ALTO_PERSONAJE);
    region[3] = new TextureRegion(p, ANCHO_PERSONAJE * 3, ALTO_PERSONAJE * direccion, ANCHO_PERSONAJE, ALTO_PERSONAJE);

    return region;
}

LOGICAL CLASS:

public void mover(Personaje p, SpriteBatch batch, String nombre_personaje, int dir, boolean moviendose, boolean corriendo) {
    //boolean colision = comprobarColision(dir, x, y);
    if (corriendo) {
        velocidad = 3;
    } else {
        velocidad = 1;
    }


    TextureRegion[] personaje = p.getPersonaje(nombre_personaje, dir);

    Animation animacion = new Animation(1 / 5f, personaje);

    if (moviendose /*&& !colision*/) {
        if (dir == DIR_DERECHA) {
            x += velocidad;
        } else if (dir == DIR_ARRIBA) {
            y += velocidad;
        } else if (dir == DIR_IZQUIERDA) {
            x -= velocidad;
        } else {
            y -= velocidad;
        }

        delta += Gdx.graphics.getDeltaTime();
        batch.draw(animacion.getKeyFrame(delta, true), x, y);
    } else
        batch.draw(personaje[0], x, y);
}

MAIN CLASS:

@Override
public void create() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    camara = new OrthographicCamera();
    camara.setToOrtho(false, w, h);
    camara.update();

    mapa = new TmxMapLoader().load("mapa.tmx");
    renderer = new OrthogonalTiledMapRenderer(mapa);

    batch = new SpriteBatch();
    batch.setProjectionMatrix(camara.combined);

    l = new Logica();
    p = new Personaje();
 }

@Override
public void render() {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    camara.update();
    renderer.setView(camara);
    renderer.render();

    batch.begin();
    l.mover(p, batch, "Prota", direccion, pulsado, turbo);
    batch.end();
}
    
asked by Sergio Domínguez Triñanes 10.11.2016 в 17:39
source

1 answer

1

You use TextureRegion or Sprite the order in which if you draw is as you put it. If you want to draw the character behind the tall objects, you could make a new Layer in TiledMap and place these "tall" objects.

When rendering, let's say you have a total of 3 layers {0,1,2} and that 1 (in the array) is that of tall objects.

//Todos los layers excepto el de los objetos 
private int[] layers = {0,2};
//El layer de los objetos altos
private int[] objetosAltosLayer = {1};
@Override
public void render() {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    camara.update();
    renderer.setView(camara);

    //aqui dibujamos todos los layers excepto el de los objetos altos
    renderer.render(layers); 

    //dibujas el personaje
    batch.begin();
    l.mover(p, batch, "Prota", direccion, pulsado, turbo);
    batch.end();

    //dibujas los objetos altos
    renderer.render(objetosAltosLayer);
}

Of course, this is not the final solution because sometimes you want to draw the character after the tall objects ... here is an image for you to see the logic ...

    
answered by 14.11.2016 в 05:46