下面是cocosStudio UI编辑器做的一个示意图,意图就是拼一个地图路径,然后通过导出路径信息,在代码中动态加载场景关卡信息。
这是导出的.csd文件内容,其实就是一个.xml文件
上面的内容其实是想让大家有个直观的印象,下面废话不多说,上干货:
#!/usr/bin/env python
#coding=utf-8
import xml.dom.minidom
import codecs
import json
import os
def coordinateExporter(filePath, jsonPath):
#data = {}
data = []
for path in filePath:
levels = []
dom = xml.dom.minidom.parse(path)
root = dom.documentElement
nodes = root.getElementsByTagName('AbstractNodeData')
for node in nodes:
name = node.getAttribute('Name')
position = node.getElementsByTagName('Position')[0]
x = position.getAttribute('X')
y = position.getAttribute('Y')
if name.find('_') != -1:
_, nodeId = name.split('_')
if name.startswith('lv_'):
levels.append({'id':nodeId, 'x':float(x), 'y':float(y)})
content = {'levels':levels}
fileName, _ = os.path.splitext(os.path.basename(path))
print fileName
data.append(content)
#data[fileName] = content
fp = codecs.open(jsonPath, 'w', 'utf-8')
json.dump(data, fp, ensure_ascii=False, indent=2, separators=(',', ': '), sort_keys=True)
fp.close()
if __name__ == '__main__':
coordinateExporter(['Test.csd',], 'coordinates.json')
#print json.dumps({'4':5, '6':7}, sort_keys = True, indent=2, separators=(',', ':'))
[
{
"levels": [
{
"id": "1001",
"x": 158.7037,
"y": 403.4228
},
{
"id": "1002",
"x": 361.1236,
"y": 225.0609
},
{
"id": "1003",
"x": 583.2164,
"y": 427.153
},
{
"id": "1004",
"x": 831.5454,
"y": 242.2315
},
{
"id": "1005",
"x": 1082.0551,
"y": 415.8986
}
]
}
]
通过对 .json 文件进行解析,就可以为我们程序所用了!