【原】Libgdx加载Tiled实现遮挡效果

最近使用Tiled时没有找到官方的Tiled地图的遮挡效果(本人才疏学浅)。。。于是自己来实现。。。

1、使用Tiled创建TMX地图

  这是没有遮挡时候的效果

  

  我有在其上添加了遮挡层 块层3

  

  大家可以发现地图中有一颗树遮挡了后面的地面以及花草。

2、处理TMX文件

  这个使用Libgdx自带的extensions下的gdx-tiled-preprocessor工具来处理 使用命令行 有源码,可以自己查看下  基本上就是两个参数 

  gdx-tiled-preprocessor   inputDir outputDir

3、加载TMX地图

  

    Stage stage; 
    float width; 
    float height; 
    private TiledMap map; 
    private TileAtlas atlas; 
    private TileMapRenderer tileMapRenderer;
    
    private SpriteBatch batch;

    Vector3 camDirection = new Vector3(1, 1, 0); 
    Vector2 maxCamPosition = new Vector2(0, 0);

    Image image;

    int[] i1 = {0,1};

    int[] i2= {2};
    

 

 1         final String path = StaticData.RESOURCE_MAP;// map/ 
 2         final String mapname = "map-1"; 
 3         FileHandle mapHandle = Gdx.files.internal(path + mapname + ".tmx"); Paht:assets/map/map-1.tmx
 4         map = TiledLoader.createMap(mapHandle); 
 5         atlas = new TileAtlas(map, Gdx.files.internal(path)); 
 6         tileMapRenderer = new TileMapRenderer(map, atlas, 10, 10); 
 7         maxCamPosition.set(tileMapRenderer.getMapWidthUnits(), tileMapRenderer 
 8                 .getMapHeightUnits()); 
 9         
10         width = Gdx.graphics.getWidth(); 
11         height = Gdx.graphics.getHeight(); 
12         stage = new Stage(width, height, true);
13         
14         batch = new SpriteBatch();//用于渲染自己的角色
15         
16         Gdx.input.setInputProcessor(stage);

原本渲染TMX

        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
        OrthographicCamera c = (OrthographicCamera) stage.getCamera(); 
        stage.act(Gdx.graphics.getDeltaTime()); 
        tileMapRenderer.render(c);
        stage.draw()

现在分层渲染

1         Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
2         OrthographicCamera c = (OrthographicCamera) stage.getCamera(); 
3         stage.act(Gdx.graphics.getDeltaTime()); 
4         tileMapRenderer.render(c,i1);
5         batch.begin();
6         Assets.actor.draw(batch);
7         batch.end();
8         tileMapRenderer.render(c,i2);
9         stage.draw(); 

最终效果:

  

鉴于本人才疏学浅,各位前辈如果有更好的办法,请不吝赐教,拜谢。

posted @ 2013-02-21 17:02  nzlov  阅读(1978)  评论(0编辑  收藏  举报