cocossharp TilePropertiesForGID(info.Gid) 总是 return null

cocossharp TilePropertiesForGID(info.Gid) always return null

我正在使用 CocoSharp.PCL.Shared Nuget 版本 1.6.2,我正在尝试获取磁贴 属性。

这里是TileSet.tsx:

<?xml version="1.0" encoding="UTF-8"?>
<tileset version="1.2" tiledversion="1.2.1" name="wood_tileset" tilewidth="32" tileheight="32" tilecount="256" columns="16">
 <image source="wood_tileset.png" width="512" height="512"/>
 <tile id="68">
  <properties>
   <property name="IsTreasure" value="true"/>
  </properties>
 </tile>
</tileset>

我调用的函数来获取我的 属性:

void HandleCustomTilePropertyAt(int worldX, int worldY, CCTileMapLayer layer)
    {
        CCTileMapCoordinates tileAtXy = layer.ClosestTileCoordAtNodePosition(new CCPoint(worldX, worldY));

        CCTileGidAndFlags info = layer.TileGIDAndFlags(tileAtXy.Column, tileAtXy.Row);

        if (info != null && info.Gid == 68)
        {
            Dictionary<string, string> properties = null;

            try
            {
                properties = tileMap.TilePropertiesForGID(info.Gid);
            }
            catch
            {
                // CocosSharp 
            }

            if (properties != null && properties.ContainsKey("IsTreasure") && properties["IsTreasure"] == "true" )
            {
                layer.RemoveTile(tileAtXy);

                // todo: Create a treasure chest entity
            }
        }
    }

问题是:properties = tileMap.TilePropertiesForGID(info.Gid); 总是 return 空。

但是如果我打破并查看非 public 变量,我可以看到我的瓷砖的 属性 :

我做错了什么?

我找到了获取磁贴属性的方法。

  1. 从 tileset.tsx
  2. 获取流
  3. 从特定图块 ID 获取属性
  4. 创建一个解析 xml 文件的函数 tileset.tsx 以使其像以前的 cocossharp 一样工作。

Program.cs中,由于它继承自activity,我可以打开所有资产:

public class Program : AndroidGameActivity
{

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        CCApplication application = new CCApplication();
        application.ApplicationDelegate = new AppDelegate();

        this.SetContentView(application.AndroidContentView);
        this.LoadTileMapProperty();

        application.StartGame();
    }

    private void LoadTileMapProperty()
    {
        Settings.TileMapStream = new StreamReader(Assets.Open("Content/tileMap/wood_tileset.tsx"));
    }

}

在我的 GameLayer.cs 中:

 void HandleCustomTilePropertyAt(int worldX, int worldY, CCTileMapLayer layer)
    {
        CCTileMapCoordinates tileAtXy = layer.ClosestTileCoordAtNodePosition(new CCPoint(worldX, worldY));

        CCTileGidAndFlags info = layer.TileGIDAndFlags(tileAtXy.Column, tileAtXy.Row);

        if (info != null && info.Gid == 68)
        {
            Dictionary<string, string> properties = null;

            try
            {
                properties = tileMap.TilePropertiesForTileID(info.Gid);
            }
            catch
            {
                // CocosSharp 
            }

            if (properties != null && properties.ContainsKey("IsTreasure") && properties["IsTreasure"] == "true")
            {
                //test adding entity via tileMap
                reliefLayer.AddChild(new Tree(tileAtXy), 3);
            }
        }
    }

然后我用这个函数替换了Cocossharp库给的函数:

public Dictionary<string, string> TilePropertiesForTileID(short tileGid)
    {
        Dictionary<string, string> propertiesDict = new Dictionary<string, string>();

        try
        {
            // Loading from a file, you can also load from a stream
            var xmlDoc = XDocument.Load(Settings.TileMapStream);

            // Query the data and write out a subset of contacts
            var propertiesQuery = xmlDoc.Root.Descendants("tile")
                                        .Where(item => (int)item.Attribute("id") == tileGid)
                                        .SelectMany(a => a.Descendants("property"))
                                        .Select(property => new
                                        {
                                            Name = property.Attribute("name").Value,
                                            Value = property.Attribute("value").Value
                                        })
                                        .ToList();

            foreach (var property in propertiesQuery)
            {
                Console.WriteLine($"Property Name: {property.Name}, Value: {property.Value}");
                propertiesDict.Add(property.Name, property.Value);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }


        return propertiesDict;
    }