1

我正在开发一个 iOS 游戏,它使用 Cocos2dTMXTiledMap来读取 Tiled Application 中生成的等距地图。

在 Tiled 中,您可以为 tileset 中的每个图像添加属性(即显示在屏幕右下角的图像)

对我来说,使用这些属性来帮助确定此图块类型是否可由游戏角色遍历是有意义的。

例如,如果图块 3,5 使用的是草图像,那么陆基角色可以走到那里。

相反,如果图块 4,8 使用水的图像,则陆基角色无法走到那里。

我曾希望通过在草地和水砖上创建一个属性来实现这一点,称为terrain_type土地 0 和水 1。然后(我曾希望)我可以在运行时访问 tile 3,5 并且以某种方式知道 tile 3,5 使用了具有以下属性的草图像terrain_type=0

现在,我意识到还有其他技术可用于完成相同的事情(想到对象层),但这似乎是最好的方法。特别是当您添加多个瓷砖层并且您想知道瓷砖 3,5 上面既有草又有墙时。

我的问题:这可能吗?我将如何去做。或者,我是否误解了 Tiled 和TMXTiledMap应该如何工作?

非常感激...

4

1 回答 1

2

惊人。在我发布这个问题之前,我花了很多时间试图让它发挥作用,当然,几个小时后我就明白了。关键是使用 CCTMXMapInfo 类。

无论如何,这是解决方案,因为我认为这对其他人有用:

  1. 在 Tiled 应用程序中创建一个具有名为“bottom”的切片图层的地图
  2. 通过右键单击每个图像并选择“平铺属性”,在名为“平铺集”(右下角)的部分中为每个平铺图像添加一个属性
  3. 将属性命名为“terrain_type”并将值设置为您喜欢的任何值(例如,terrain_type = 0 表示陆地或terrain_type = 1 表示水)
  4. 使用这些图像绘制您的瓷砖地图并保存

使用此代码读取位置 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"];
于 2013-02-13T06:07:54.967 回答