【问题标题】:hex grid A* pathfinding... type argument error十六进制网格 A* 寻路...类型参数错误
【发布时间】:2012-05-31 09:02:32
【问题描述】:

我是一名初出茅庐的编程爱好者和游戏设计师,正在攻读学位,因此在编程世界中我还是个新手。我已经完成了大量的 JavaScript(实际上是 UnityScript),现在正尝试涉足 C#。我一直在关注 hakimio 的在 Unity 中制作回合制 RPG 的教程,该教程基于使用 A* 寻路的六边形网格。 (http://tbswithunity3d.wordpress.com/)

我的问题是,我已经按照他的教程一步一步完成了 A* 寻路脚本和资产,但在 Unity 中遇到了错误:

“错误 CS0308:非泛型类型 `IHasNeighbours' 不能与类型参数一起使用”

这是引发错误消息的代码,位于public class Tile: GridObject, IHasNeighbours<Tile> 行:

using System.Collections.Generic;
using System;
using System.Linq;
using UnityEngine;

public class Tile: GridObject, IHasNeighbours<Tile>
{
public bool Passable;

public Tile(int x, int y)
    : base(x, y)
{
    Passable = true;
}

public IEnumerable AllNeighbours { get; set; }
public IEnumerable Neighbours
{
    get { return AllNeighbours.Where(o => o.Passable); }
}

public static List<Point> NeighbourShift
{
    get
    {
        return new List<Point>
        {
            new Point(0, 1),
            new Point(1, 0),
            new Point(1, -1),
            new Point(0, -1),
            new Point(-1, 0),
            new Point(-1, 1),
        };
    }
}
public void FindNeighbours(Dictionary<Point, Tile> Board, Vector2 BoardSize, bool EqualLineLengths)
{
    List<Tile> neighbours = new List<Tile>();

    foreach (Point point in NeighbourShift)
    {
        int neighbourX = X + point.X;
        int neighbourY = Y + point.Y;
        //x coordinate offset specific to straight axis coordinates
        int xOffset = neighbourY / 2;

        //if every second hexagon row has less hexagons than the first one, just skip the last one when we come to it
        if (neighbourY % 2 != 0 && !EqualLineLengths && neighbourX + xOffset == BoardSize.x - 1)
            continue;
        //check to determine if currently processed coordinate is still inside the board limits
        if (neighbourX >= 0 - xOffset &&
            neighbourX < (int)BoardSize.x - xOffset &&
            neighbourY >= 0 && neighbourY < (int)BoardSize.y)
            neighbours.Add(Board[new Point(neighbourX, neighbourY)]);
    }

    AllNeighbours = neighbours;
}
}

任何有关如何克服此错误的帮助或见解将不胜感激,过去几天我一直在努力解决这个脚本,试图让它工作,并且无法继续本教程(以及我的项目) 出现错误。

提前谢谢大家!

亚伦:)

【问题讨论】:

  • 嗨亚伦。快速说明一下,我编辑了您的帖子以解决您评论的需要空间来处理 &lt;&gt; 的问题。在工具栏上发布时,您应该会看到一个“代码”按钮。这会将文本标记为代码,并确保它显示所有文本并对其进行格式化。您已将它用于主要批量,但也可以内联使用。

标签: c# path-finding a-star generic-type-argument hexagonal-tiles


【解决方案1】:

问题在于 IHasNeighbours 不是通用接口,因此您无法像传递 Tile 类那样将类传递给它。

要么您需要修改您的 IHasNeighbours 接口以使其具有通用性,要么您需要在它之后取出对 Tile 类的引用。解决方案将取决于您需要代码执行的操作。 :)

【讨论】:

  • 感谢@Chris 的快速回复,但我仍然不确定如何解决问题。我尝试删除“”,然后它开始使用 IEnumerator (*IEnumerable) 列出其他相同类型的错误。我只是不知道能够解决这个问题......
  • 好吧,看看这个错误,如果删除&lt;Tile&gt; 解决了它(使用反引号` 标记代码的东西)那么你的界面不是通用的。您可能需要阅读泛型以了解它们是什么,何时需要它们以及何时应该使用泛型类型参数(例如&lt;type&gt;)。例如AllNeighboursNeighbours 可能希望成为IEnumerable&lt;Tile&gt; 而不是普通的IEnumerable。如果这是来自您的界面,那么您的解决方案可能是将您的界面修改为通用的(发布代码,我们可以看看)。
  • 又看了一圈后,我发现 Aron Granberg 的 A* 寻路包可以用于六角网格('John' redclovergames.com/blog/?p=563),而且我还可以适应它在移动平台上使用触摸控件而不是鼠标单击,这非常适合我的需要。感谢您的帮助克里斯 :)
猜你喜欢
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
  • 2013-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多