简体   繁体   中英

Android LibGDX: tiled map covers animation

I have a tiled map and an animation. However, the map covers this last and I don't know why. If I add only the animation, I can see it, but not if also I add the map. Please answer me, I can't find the solution!

Here is my code:

public class MyGdxGame extends ApplicationAdapter implements GestureDetector.GestureListener
{

Texture texture;
SpriteBatch batch;
Sprite sprite;
TextureAtlas textureAtlas;
Animation animation;
float time;
OrthographicCamera orthographicCamera;

Texture corridore;
Sprite spriteCorridore;

TiledMap map;
OrthogonalTiledMapRenderer renderer;

@Override
public void create () {

    batch = new SpriteBatch();
    corridore = new Texture("a.png");
    spriteCorridore = new Sprite(corridore);
    orthographicCamera = new OrthographicCamera(500,500);
    orthographicCamera.position.x=500;
    orthographicCamera.position.y=250;
    map = new TmxMapLoader().load("mappa.tmx");
    renderer = new OrthogonalTiledMapRenderer(map);
    textureAtlas = new TextureAtlas(Gdx.files.internal("corsa.atlas"));
    animation = new Animation(1/20f,textureAtlas.findRegion("a"),textureAtlas.findRegion("b"),
            textureAtlas.findRegion("c"),textureAtlas.findRegion("d"),textureAtlas.findRegion("e"),
            textureAtlas.findRegion("f"),textureAtlas.findRegion("g"),textureAtlas.findRegion("h"),
            textureAtlas.findRegion("i"));
}

@Override
public void render () {
    time = time + Gdx.graphics.getDeltaTime();
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.setProjectionMatrix(orthographicCamera.combined);
    batch.begin();
    batch.draw((TextureRegion)animation.getKeyFrame(time,true),orthographicCamera.position.x-spriteCorridore.getWidth()/2,orthographicCamera.position.y-spriteCorridore.getHeight()/2);
    renderer.setView(orthographicCamera);
    renderer.render();

    orthographicCamera.translate(1, 0);
    orthographicCamera.update();

    batch.end();
}

}

Render your animation at the top of TiledMapRenderer so change your rendering order.

@Override
public void render () {
    time = time + Gdx.graphics.getDeltaTime();
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    renderer.setView(orthographicCamera);
    renderer.render();

    batch.setProjectionMatrix(orthographicCamera.combined);
    batch.begin();
    batch.draw((TextureRegion)animation.getKeyFrame(time,true),orthographicCamera.position.x-spriteCorridore.getWidth()/2,orthographicCamera.position.y-spriteCorridore.getHeight()/2);
    batch.end();

    orthographicCamera.translate(1, 0);
    orthographicCamera.update();   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM