Skip to content

Commit

Permalink
Merge pull request #722 from dingpinglv/Iss719_TMXSupportXMLANDCSV
Browse files Browse the repository at this point in the history
Fixed #719 TMXXMLParser support XML and CSV format
  • Loading branch information
SeanLin committed Dec 28, 2012
2 parents 48fc3f6 + b6853d3 commit a02ffc4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
2 changes: 0 additions & 2 deletions cocos2d/sprite_nodes/CCSprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ cc.cutRotateImageToCanvas = function (texture, rect) {
ctx.translate(nCanvas.width / 2, nCanvas.height / 2);
ctx.rotate(-1.5707963267948966);
ctx.drawImage(texture, rect.origin.x, rect.origin.y, rect.size.height, rect.size.width, -rect.size.height / 2, -rect.size.width / 2, rect.size.height, rect.size.width);
var img = new Image();
img.src = nCanvas.toDataURL();
return nCanvas;
};

Expand Down
38 changes: 24 additions & 14 deletions cocos2d/tileMap_parallax_nodes/CCTMXXMLParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ cc.TMXTilesetInfo = cc.Class.extend(/** @lends cc.TMXTilesetInfo# */{
var rect = cc.RectZero();
rect.size = this._tileSize;
gid &= cc.TMX_TILE_ALL_FLAGS_MASK;
gid = gid - parseInt(this.firstGid,10);
var max_x = parseInt((this.imageSize.width - this.margin * 2 + this.spacing) / (this._tileSize.width + this.spacing),10);
rect.origin.x = parseInt((gid % max_x) * (this._tileSize.width + this.spacing) + this.margin,10);
rect.origin.y = parseInt(parseInt(gid / max_x,10) * (this._tileSize.height + this.spacing) + this.margin,10);
gid = gid - parseInt(this.firstGid, 10);
var max_x = parseInt((this.imageSize.width - this.margin * 2 + this.spacing) / (this._tileSize.width + this.spacing), 10);
rect.origin.x = parseInt((gid % max_x) * (this._tileSize.width + this.spacing) + this.margin, 10);
rect.origin.y = parseInt(parseInt(gid / max_x, 10) * (this._tileSize.height + this.spacing) + this.margin, 10);
return rect;
}
});
Expand Down Expand Up @@ -524,12 +524,12 @@ cc.TMXMapInfo = cc.SAXParser.extend(/** @lends cc.TMXMapInfo# */{
var t = tiles[i];
this.setParentGID(parseInt(info.firstGid) + parseInt(t.getAttribute('id') || 0));
var tp = t.querySelectorAll("properties > property");
if(tp){
var dict = {};
for(var j = 0; j < tp.length; j++) {
var name = tp[j].getAttribute('name');
var value = tp[j].getAttribute('value');
dict[name] = value;
if (tp) {
var dict = {};
for (var j = 0; j < tp.length; j++) {
var name = tp[j].getAttribute('name');
var value = tp[j].getAttribute('value');
dict[name] = value;
}
this._tileProperties[this.getParentGID()] = dict;
}
Expand Down Expand Up @@ -563,7 +563,7 @@ cc.TMXMapInfo = cc.SAXParser.extend(/** @lends cc.TMXMapInfo# */{
layer.offset = cc.p(parseFloat(selLayer.getAttribute('x')) || 0, parseFloat(selLayer.getAttribute('y')) || 0);

var nodeValue = '';
for ( j = 0; j < data.childNodes.length; j++) {
for (j = 0; j < data.childNodes.length; j++) {
nodeValue += data.childNodes[j].nodeValue
}
nodeValue = nodeValue.trim();
Expand All @@ -577,16 +577,26 @@ cc.TMXMapInfo = cc.SAXParser.extend(/** @lends cc.TMXMapInfo# */{
layer._tiles = cc.unzipBase64AsArray(nodeValue, 4);
break;
case 'zlib':
var inflator = new Zlib.Inflate(cc.Codec.Base64.decodeAsArray(nodeValue,1));
var inflator = new Zlib.Inflate(cc.Codec.Base64.decodeAsArray(nodeValue, 1));
layer._tiles = cc.uint8ArrayToUint32Array(inflator.decompress());
break;
case null:
case '':
// Uncompressed
if (encoding == "base64")
layer._tiles = cc.Codec.Base64.decodeAsArray(nodeValue, 4);
else
layer._tiles = cc.StringToArray(nodeValue);
else if (encoding === "csv") {
layer._tiles = [];
var csvTiles = nodeValue.split(',');
for (var csvIdx = 0; csvIdx < csvTiles.length; csvIdx++)
layer._tiles.push(parseInt(csvTiles[csvIdx]));
} else {
//XML format
var selDataTiles = data.getElementsByTagName("tile");
layer._tiles = [];
for(var xmlIdx = 0; xmlIdx < selDataTiles.length; xmlIdx++)
layer._tiles.push(parseInt(selDataTiles[xmlIdx].getAttribute("gid")));
}
break;
default:
cc.Assert(this.getLayerAttribs() != cc.TMX_LAYER_ATTRIB_NONE, "TMX tile map: Only base64 and/or gzip/zlib maps are supported");
Expand Down

0 comments on commit a02ffc4

Please sign in to comment.