我正在开发一个iOS游戏,它使用Cocos2d TMXTiledMap来读取在平铺应用程序中生成的等轴测地图。
在平铺中,您可以将属性添加到平铺集中的每个图像(即。显示在屏幕右下角的图像)
对我来说,使用这些属性来帮助确定游戏角色是否可以遍历这种瓦片类型是有意义的。
例如,如果瓷砖3,5使用的是草的图像,那么陆基角色就可以在那里行走。
相反,如果瓦片4,8使用的是水的图像,那么陆基角色就不能在那里行走。
我曾希望通过在草地和水上创建一个名为terrain_type的属性来实现这一点,该属性将为0表示土地,1表示水。然后(我曾希望)我可以在运行时访问tile 3,5,并以某种方式知道tile 3,5使用了具有terrain_type=0属性的草图像
现在,我意识到还有其他可用的技术来完成同样的事情(想到对象层),但这似乎是最好的方法。特别是当你添加了多个瓦片层,你想知道瓦片3,5上面既有草又有墙。
我的问题是:这可能吗?我该怎么做呢。或者,我对Tiled和TMXTiledMap的工作原理有什么误解吗?
非常感谢..。
发布于 2013-02-13 14:07:55
太棒了。在我发布这个问题之前,我花了很多时间试图让它发挥作用,当然,几个小时后我就弄明白了。关键是使用CCTMXMapInfo类。
无论如何,这是解决方案,因为我认为这可能对其他人有用:
在切片应用程序中创建具有名为"bottom“
使用以下代码读取位置3,5处的单个平铺的属性:
//read the tile map
TMXTiledMap *tileMap = [CCTMXTiledMap tiledMapWithTMXFile:@"sample_map.tmx"];
//get the bottom layer from the tileMap
CCTMXLayer *bottomLayer = [tileMap layerNamed:@"bottom"];
//get CCTMXMapInfo object -- TMXTiledMap DOES NOT Contain the tile image properties
CCTMXMapInfo * mapInfo = [CCTMXMapInfo formatWithTMXFile: @"sample_map.tmx"];
//get tile id of the tile image used at this coordinate (3, 5) in this layer
int tileID = [bottomLayer tileGIDAt: ccp(3, 5)];
//get the properties for that tile image
NSDictionary *properties = [mapInfo.tileProperties objectForKey:[NSNumber numberWithInt:tileID] ];
//get the terrain_type property
NSString *terrainType = [properties objectForKey:@"terrain_type"];https://stackoverflow.com/questions/14844034
复制相似问题