【问题标题】:Using Tileset Image Properties in a Cocos2d TMXTiledMap在 Cocos2d TMXTiledMap 中使用 Tileset 图像属性
【发布时间】:2013-01-28 10:29:41
【问题描述】:

我正在开发一个 iOS 游戏,它使用 Cocos2d TMXTiledMap 来读取 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 应该如何工作?

非常感谢...

【问题讨论】:

    标签: ios cocos2d-iphone


    【解决方案1】:

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

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

    1. 在 Tiled 应用程序中创建一个地图,其中包含一个名为 “底”
    2. 在名为的部分中为每个平铺图像添加一个属性 “Tilesets”(右下角)通过右键单击每个图像并 选择“平铺属性”
    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"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 2013-06-15
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多