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