我找到了一种获取我的瓷砖属性的方法。
-
从tileset.tsx获取流
-
从指定的磁贴ID获取属性
-
创建一个解析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;
}