【发布时间】:2018-01-03 17:59:37
【问题描述】:
我的第一个脚本是创建一个字符串列表,并在每次我的玩家或名为 pacman_1 的游戏对象之间发生冲突时更新。这是代码:
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
那么这是在一个脚本中更新的列表,而不是在另一个脚本中:
【问题讨论】:
-
你能删除所有不必要的代码吗?如果您删除了所有注释掉的代码和额外的空白,那么看起来就不那么令人生畏了。
-
@EdPlunkett 没关系,但我看到用户也因为留下注释掉的代码而受到殴打。
-
@Eddge 确实是这样,这也是我在 cmets 中提出这一点的部分原因。
-
现在不行了,抱歉保留注释的代码,这样我就可以向老师展示我的代码随着时间的推移是如何变化的
-
@EdPlunkett 是的,你之前曾试图帮助我。我只是在询问一般的想法,例如这种情况通常发生在哪里,但由于需要代码而重新发布