libgdx系列之-加载tileMap地图
?? ??? ??? ?List<AtlasRegion> atlasRegions = textureAtlas.findRegions(removeExtension(removePath(set.imageName)));
?? ??? ??? ?for (AtlasRegion reg : atlasRegions) {
?? ??? ??? ??? ?regionsMap.put(reg.index + set.firstgid, reg);
?? ??? ??? ??? ?if (!textures.contains(reg.getTexture())) {
?? ??? ??? ??? ??? ?textures.add(reg.getTexture());
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?}
修改为:通过算数来算出值,而不是通过packfile文件读取,package file读取可能会产生很多问题。
public TileAtlas (TiledMap map, FileHandle inputDir) {?
?? ??? ?for (TileSet set : map.tileSets) { ?
?? ??? ??? ?if(set.imageName==null){
?? ??? ??? ??? ?continue;
?? ??? ??? ?}
?? ??? ??? ?FileHandle imageFile = getRelativeFileHandle(inputDir, (set.imageName) );?
?? ??? ??? ?List<AtlasRegion> atlasRegions=new ArrayList<AtlasRegion>();
?? ??? ??? ?Texture texture = new Texture(imageFile);
?? ??? ??? ?for(int j=0;j<texture.getHeight()/set.tileHeight;j++){
?? ??? ??? ??? ?int wn=texture.getWidth()/set.tileWidth;
?? ??? ??? ??? ?for(int i=0;i<wn;i++){
?? ??? ??? ??? ??? ?AtlasRegion atlasRegion= new AtlasRegion(texture,i*set.tileWidth,j*set.tileHeight,set.tileWidth,set.tileHeight);
?? ??? ??? ??? ??? ?atlasRegion.index=j*wn+i;
?? ??? ??? ??? ??? ?atlasRegions.add(atlasRegion);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ??? ?for (AtlasRegion reg : atlasRegions) {
?? ??? ??? ??? ?regionsMap.put(reg.index + set.firstgid, reg);
?? ??? ??? ??? ?if (!textures.contains(reg.getTexture())) {
?? ??? ??? ??? ??? ?textures.add(reg.getTexture());
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?}
?
?
我这里有一个demo,截图如下

?
代码如下:
?
?