0
\$\begingroup\$

I'm using TildedMap to separate map layer, drawSprites layer and objects layer.I'm trying to draw the map, then the player animations and then the objects on the map. This way I'll be able to have the player walk behind an object. This works for single sprites but not for animations. When I run the game, it doesn't draw the animation and if I render the animation continuously, the object layer is never reached. What can I do to get around this?

public class PlayerRenderManager {
.....


//Animation for when the player is moving up
public Animation<TextureRegion> playerUp() {
    TextureRegion[][] tmp = TextureRegion.split(textureUp, 32,32);  
    TextureRegion[] walkUpFrames = new TextureRegion[4];
    sprite = new Sprite(textureUp);
    int index = 0;
        for (int j = 0; j < 4; j++) {
            walkUpFrames[index++] = tmp[0][j];
        }
        animation = new Animation<TextureRegion>(1f/30f,walkUpFrames);
        return animation;
}

Rendering class

public class MapRenderManager extends OrthogonalTiledMapRenderer {
......


private float stateTime;
public void render(Player player, ArrayList<Actor> actors, ArrayList<Projectiles> projectiles, PlayerRenderManager playerrender) {

    stateTime += Gdx.graphics.getDeltaTime();
    beginRender();
    int currentLayer = 0;
    for (MapLayer layer : map.getLayers()) {
        if (layer.isVisible()) {
            if (layer instanceof TiledMapTileLayer) {
                renderTileLayer((TiledMapTileLayer) layer);
                currentLayer++;

                // draw sprites if ground layer has been rendered and object layer is yet to be rendered
                if (currentLayer == drawSpritesAfterLayer) { 

                    batch.draw(playerrender.playerDown().getKeyFrame(stateTime, true),0,0);

                } else {
                    for (MapObject object : layer.getObjects()) {
                        renderObject(object);
                    }
                    for (int i = 0; i < projectiles.size(); i++) {
                        projectiles.get(i).getProjSprite().draw(this.getBatch());
                    }
                }
            }
        }
    }
    endRender();
}
\$\endgroup\$
0
\$\begingroup\$

I solved it on my own! I needed to stop rendering the map, then draw the animation before rendering the rest of the map again.

public void render(Player player, ArrayList<Actor> actors, ArrayList<Projectiles> projectiles, PlayerRenderManager playerrender) {

    beginRender();

    int currentLayer = 0;
    for (MapLayer layer : map.getLayers()) {
        if (layer.isVisible()) {
            if (layer instanceof TiledMapTileLayer) {
                renderTileLayer((TiledMapTileLayer) layer);
                currentLayer++;

                // draw sprites if ground layer has been rendered and object layer is yet to be rendered
                if (currentLayer == drawSpritesAfterLayer) { 
                    //player.getSprite().draw(this.getBatch());
                    endRender();
                    playerrender.update(player, 0,0);
                    beginRender();


                }
                else {

                    for (MapObject object : layer.getObjects()) {
                        renderObject(object);
                    }
                    for (int i = 0; i < projectiles.size(); i++) {
                        projectiles.get(i).getProjSprite().draw(this.getBatch());
                    }
                }
            }
        }
    }

    endRender();

}

The endRender() and beginRender() in the middle is all that was needed to fix it!

\$\endgroup\$
  • \$\begingroup\$ Found another solution where I did batch = this.getBatch(); \$\endgroup\$ – user125417 Mar 25 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.