【发布时间】:2015-12-17 04:36:37
【问题描述】:
我遇到了一个我可以理解但无法修复的错误,这是一个 CS0123 错误,其中方法和委托之间的参数不匹配。但是我查看了我的代码,它似乎是正确的类型,所以我很困惑并寻求您的帮助,因为我正在通过这个项目学习更高级的 C#。该项目即将生成一个六角网格,然后在两个六角图块之间对其进行 A* 寻路。我使用了this serie of written tutorials,即使它是旧的,并且必须刷新一点代码才能使其在 Unity 5 和更新版本的 C# .NET 上工作(我猜)。
以下是错误:
Assets/Scripts/GridManager.cs(151,39):错误 CS0123:方法或委托“GridManager.calcDistance(Tile)”参数与委托“System.Func()”参数不匹配
Assets/Scripts/GridManager.cs(153,17):错误 CS1502:“GridManager.DrawPath(System.Collections.Generic.IEnumerable)”的最佳重载方法匹配有一些无效参数
Assets/Scripts/GridManager.cs(153,17):错误 CS1503:参数“#1”无法将“object”表达式转换为“System.Collections.Generic.IEnumerable”类型
我很确定两个 last 的存在只是因为第一个因为它无法识别应该是 Tile 的正确 var 类型。
我希望你能帮助我并解释我做错了什么。我想我不完全是那里发生的事情。
提前谢谢你!
这是我修改过的一些代码,但和教程基本一样:
GridManager.cs:
double calcDistance(Tile tile)
{
Tile destTile = destTileTB.tile;
float deltaX = Mathf.Abs(destTile.X - tile.X);
float deltaY = Mathf.Abs(destTile.Y - tile.Y);
int z1 = -(tile.X + tile.Y);
int z2 = -(destTile.X + destTile.Y);
float deltaZ = Mathf.Abs(z2 - z1);
return Mathf.Max(deltaX, deltaY, deltaZ);
}
private void DrawPath(IEnumerable<Tile> path)
{
if (this.path == null)
this.path = new List<GameObject>();
this.path.ForEach(Destroy);
this.path.Clear();
GameObject lines = GameObject.Find("Lines");
if (lines == null)
lines = new GameObject("Lines");
foreach (Tile tile in path)
{
var line = (GameObject)Instantiate(Line);
Vector2 gridPos = new Vector2(tile.X + tile.Y / 2, tile.Y);
line.transform.position = calcWorldCoord(gridPos);
this.path.Add(line);
line.transform.parent = lines.transform;
}
}
public void generateAndShowPath()
{
if (originTileTB == null || destTileTB == null)
{
DrawPath(new List<Tile>());
return;
}
Func<Tile, Tile, double> distance = (node1, node2) => 1;
var path = PathFinder.FindPath(originTileTB.tile, destTileTB.tile,
distance, calcDistance); //error is here
DrawPath(path);
}
PathFinder.cs:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public static class PathFinder
{
public static Path<Node> FindPath<Node>(
Node start,
Node destination,
Func<Node, Node, double> distance,
Func<Node> estimate)
where Node : IHasNeighbours<Tile>
{
var closed = new HashSet<Node>();
var queue = new PriorityQueue<double, Path<Node>>();
queue.Enqueue(0, new Path<Node>(start));
while (!queue.IsEmpty)
{
var path = queue.Dequeue();
if (closed.Contains(path.LastStep))
continue;
if (path.LastStep.Equals(destination))
return path;
closed.Add(path.LastStep);
foreach (Node n in path.LastStep.Neighbours)
{
double d = distance(path.LastStep, n);
var newPath = path.AddStep(n, d);
queue.Enqueue(newPath.TotalCost + estimate(n), newPath);
}
}
return null;
}
}
【问题讨论】:
标签: c# unity3d types delegates