首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >cocossharp (info.Gid)总是返回null

cocossharp (info.Gid)总是返回null
EN

Stack Overflow用户
提问于 2019-01-08 00:01:28
回答 1查看 68关注 0票数 1

我正在使用CocoSharp.PCL.Shared NugetVersion1.6.2,并且我正在尝试获取该属性。

这里是TileSet.tsx:

代码语言:javascript
复制
<?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>

获取属性所调用的函数:

代码语言:javascript
复制
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);总是返回null。

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

我做错什么了?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-11 14:59:34

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

  1. 从tileset.tsx获取一个流
  2. 从特定的标识中获取属性
  3. 创建一个解析xml文件tileset.tsx的函数,使其像用于工作的cocossharp一样工作。

Program.cs中,由于它继承了活动,所以我能够打开所有资产:

代码语言:javascript
复制
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

代码语言:javascript
复制
 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库提供的函数:

代码语言:javascript
复制
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;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54083566

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档