我的第一个脚本是每次我的播放器或名为pacman_1的gameObject之间发生冲突时创建字符串和更新列表。这是代码:
public class Node : MonoBehaviour
{
public List<string> FinalPath = new List<string>();
public static GameObject PlayerPos, EnemyPos;
public static string SPlayerPos, SEnemyPos;
public static bool E, P;
// Use this for initialization
void Start()
{
var box = gameObject.AddComponent<BoxCollider2D>();
box.isTrigger = enabled;
private void Update()
{
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.name == "pacman_1")
{
SEnemyPos = EnemyPos.name
E = true;
}
if (other.name == "player")
{
SPlayerPos = PlayerPos.name;
P = true;
}
if (P == true || E == true)
{
Graph g = new Graph();
g.add_vertex("P1", new Dictionary<string, int>() { { "P41", 3 }, { "P49", 2 } });
/***********************************/
/**** 62 vertices omitted -- ed ****/
/***********************************/
g.add_vertex("P64", new Dictionary<string, int>() { { "P34", 3 }, { "P44", 2 } });
g.shortest_path(SPlayerPos, SEnemyPos).ForEach(x => FinalPath.Add(x));
E = false;
P = false;
}
}
}
public class Graph
{
Dictionary<string, Dictionary<string, int>> vertices = new Dictionary<string, Dictionary<string, int>>();
public void add_vertex(string name, Dictionary<string, int> edges)
{
vertices[name] = edges;
}
public List<string> shortest_path(string start, string finish)
{
var previous = new Dictionary<string, string>();
var distances = new Dictionary<string, int>();
var nodes = new List<string>();
List<string> path = null;
foreach (var vertex in vertices)
{
if (vertex.Key == start)
{
distances[vertex.Key] = 0;
}
else
{
distances[vertex.Key] = int.MaxValue;
}
nodes.Add(vertex.Key);
}
while (nodes.Count != 0)
{
nodes.Sort((x, y) => distances[x] - distances[y]);
var smallest = nodes[0];
nodes.Remove(smallest);
if (smallest == finish)
{
path = new List<string>();
while (previous.ContainsKey(smallest))
{
path.Add(smallest);
smallest = previous[smallest];
}
break;
}
if (distances[smallest] == int.MaxValue)
{
break;
}
foreach (var neighbor in vertices[smallest])
{
var alt = distances[smallest] + neighbor.Value;
if (alt < distances[neighbor.Key])
{
distances[neighbor.Key] = alt;
previous[neighbor.Key] = smallest;
}
}
}
path.Add(start);
return path;
}
}
那么这是我的第一次正确访问列表的其他脚本,但在此之后保持不变:
public class enemy : MonoBehaviour
{
List<GameObject> finP = new List<GameObject>();
public Node nodeScript;
public float Speed = 1.0f;
// Use this for initialization
void Start()
{
nodeScript = GameObject.Find("P16").GetComponent<Node>();
}
// Update is called once per frame
void Update()
{
MovePosition();
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "pallet" && nodeScript.FinalPath.Count>1)
{
finP.Clear();
foreach(string var in nodeScript.FinalPath)
{
finP.Add(GameObject.Find(var));
}
}
}
void MovePosition()
{
if (finP.Count < 1) return;
transform.localPosition = Vector3.MoveTowards(transform.localPosition, finP[1].transform.localPosition, Speed * Time.deltaTime);
}
}
这是在我的控制台中按预期输出的原始列表: screen shot of console
然后这是一个脚本中更新的列表,而不是另一个脚本中的列表:
答案 0 :(得分:0)
您要更新的List<string> FinalPath
是?如果是这样,请尝试在Start()函数中初始化它,如下所示:
void Start(){
FinalPath = new List<string>();
}
我不确定,但我认为列表无法在方法之外进行初始化。
答案 1 :(得分:0)
我会查看一些方面,看看它们是否与您脚本的预期结果相冲突。
nodeScript = GameObject.Find("P16").GetComponent<Node>();
这是Node
上的GameObject
组件,其名称为“P16”,因此nodeScript.FinalPath
调用只会引用该节点。这是有意的吗?可能是'P16'节点只收到一定数量的碰撞。
nodeScript.FinalPath.Count>1
当列表中至少有两个元素时,返回true;你打算进行> 0
手术吗?
foreach(string var in nodeScript.FinalPath)
{
finP.Add(GameObject.Find(var));
}
var
是C#中的关键字,我会避免将其用作变量名。虽然看起来编译好了。