LGameEngine 中加载tmx失败的愿因

最近在研究LGameEngine,在试用tmx地图的时候老是显示tmx解析失败,我试图用j2me版本的代码调试,但是发现好像是文件找不到的问题,但是android上面应该是可以找到assets下面的资源的啊,被整糊涂了;一度很郁闷就去找别的engine看,在看AndEngine的时候,意外看到有位老兄跟我有相同的经历,我一路看下来原来现在Tiled编辑器生成的代表tileID的数据被压缩的模式错了,LGameEngine下面解析Tmx文件中的数据部分(其实就是map中的哪一格用什么tile)是按照gzip压缩方式进行解析的,而Tile默认用得是zlib方式进行压缩的,所以解析就错啦。

LGameEngine里面解析Tmx的代码在TmxLayer.java中,代码如下:

<java>

public TMXLayer(TMXTiledMap map, Element element) throws RuntimeException {
this.map = map;
name = element.getAttribute("name");
width = Integer.parseInt(element.getAttribute("width"));
height = Integer.parseInt(element.getAttribute("height"));
data = new int[width][height][3];


// 获得当前图层属性
Element propsElement = (Element) element.getElementsByTagName(
"properties").item(0);
if (propsElement != null) {
NodeList properties = propsElement.getElementsByTagName("property");
if (properties != null) {
props = new TMXProperty();
for (int p = 0; p < properties.getLength(); p++) {
Element propElement = (Element) properties.item(p);


String name = propElement.getAttribute("name");
String value = propElement.getAttribute("value");
props.setProperty(name, value);
}
}
}


Element dataNode = (Element) element.getElementsByTagName("data").item(
0);
String encoding = dataNode.getAttribute("encoding");
String compression = dataNode.getAttribute("compression");


// 进行base64的压缩解码
if ("base64".equals(encoding) && "gzip".equals(compression)) {
try {
Node cdata = dataNode.getFirstChild();
char[] enc = cdata.getNodeValue().trim().toCharArray();
byte[] dec = decodeBase64(enc);
GZIPInputStream is = new GZIPInputStream(
new ByteArrayInputStream(dec));


for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int tileId = 0;
tileId |= is.read();
tileId |= is.read() << 8;
tileId |= is.read() << 16;
tileId |= is.read() << 24;


if (tileId == 0) {
data[x][y][0] = -1;
data[x][y][1] = 0;
data[x][y][2] = 0;
} else {
TMXTileSet set = map.findTileSet(tileId);


if (set != null) {
data[x][y][0] = set.index;
data[x][y][1] = tileId - set.firstGID;
}
data[x][y][2] = tileId;
}
}
}
} catch (IOException e) {
throw new RuntimeException("Unable to decode base64 !");
}
} else {
throw new RuntimeException("Unsupport tiled map type " + encoding
+ "," + compression + " only gzip base64 Support !");
}
}

</java>

你可能感兴趣的:(properties,String,null,j2me,encoding,compression)