求大佬帮助,我尝试了官方文档、搜索引擎、github源码…都没有找到相关信息,悲伤。。。
文档里有啊 layer.getTileGIDAt 这个接口获取tmx文件中每个块的id,根据id就知道了啊
let map = this.node.getComponent(cc.TiledMap);
let wayNode = map.getLayer(“way”); //路点层级信息
let wayLayer = wayNode.getComponent(cc.TiledLayer);
let size = wayLayer .getLayerSize(); // width = 12, height = 8
let mapSize = wayLayer .getMapTileSize(); //width = 80, height = 80
let curWidth = 0;
let curHeight = 0;
let data = [];
while (curHeight <= size.height-1){
// cc.error("cur height ",curHeight);
while (curWidth <= size.width-1)
{
let num = layer.getTileGIDAt(cc.v2(curWidth, curHeight));
if(num != 0){ //找到建筑物了
let pos = cc.v2(curWidthmapSize.width, curHeightmapSize.height);
data.push({id: num, x: pos.x+mapSize.width/2, y: mapHeight-pos.y-mapSize.height/2}); //插入数据,右上角坐标系转成左下角
}
curWidth += 1;
}
curWidth = 0;
curHeight+=1;
}
感谢您的帮助,GID代表地图块ID,但是我需要通过GID关联的terrain的信息。难道只能自己解析tmx文件的terraintypes信息吗?
<terraintypes> <terrain name="平原" tile="1"/> <terrain name="草地" tile="2"/> <terrain name="城池" tile="3"/> <terrain name="营寨" tile="4"/> <terrain name="森林" tile="5"/> <terrain name="山地" tile="6"/> <terrain name="河流" tile="16"/> <terrain name="村庄" tile="41"/> </terraintypes>
我们用的版本可能不一样,我tmx文件并没有terraintypes这个玩意,我的格式是这样的
0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,5,62,62,0,62,62,62,0,62,62,0,0,
0,0,0,0,0,62,0,0,0,62,0,0,0,0,
0,0,0,0,0,62,0,62,0,62,0,5,0,0,
0,0,0,0,0,0,0,62,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0
tiled editor编辑器中自己定义地图块id 代码解析出来的id自然知道是什么了
不知道为啥一部分内容被自动删掉了… 我的格式就是一个layer 下边跟的这种数组
我明白你的意思了,非常感谢您的回复,
我的场景是多个地图块ID(GID)对应一种地形(Terrain),
看来Cocos creator的TiledMap这个关联关系(tiled editor是支持的),只能手动创建这个关联关系。
嗯…你明白了就好