代码之家  ›  专栏  ›  技术社区  ›  Christophe Chenel

cocsharp tilePropertiesForgid(info.gid)始终返回空值

  •  1
  • Christophe Chenel  · 技术社区  · 6 年前

    我正在使用cocosharp.pcl.shared nuget版本1.6.2,我正在尝试获取tile属性。

    以下是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); 总是返回空值。

    但是,如果我中断并研究非公共变量,我可以看到我的tile的属性: enter image description here

    我做错什么了?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Christophe Chenel    6 年前

    我找到了一种获取我的瓷砖属性的方法。

    1. 从tileset.tsx获取流
    2. 从指定的磁贴ID获取属性
    3. 创建一个解析xml文件tileset.tsx的函数,使其像cocsharp一样工作。

    程序CS ,因为它继承自活动,所以我可以打开所有资产:

    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"));
        }
    
    }
    

    在我的 魔芋素 :

     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);
                }
            }
        }
    

    然后我将cocsharp库给出的函数替换为以下函数:

    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;
        }
    
    推荐文章