- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的统一脚本中有一个非常简单或非常复杂的问题。简单地说,当尝试在下面显示的方法中使用打开列表上的 .Add 或 .Insert 方法时,Unity 编辑器停止响应任何输入并挂起,直到在任务管理器中强制关闭。
有问题的方法:
private void addToOpen(location l){ //causes editor crash
int fScore = l.getF (); //f score of the location to add
int count = open.Count; //number of values in the list
int index = 0; //index to insert the location
Debug.Log ("count " + count);
while (index < count) {
if(fScore >= open[index].getF()){
break;
}
index++;
}
open.Insert(index, temp);
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //for lists
using System; //for broad use of "Exception"
public class MouseBasedMovement : MonoBehaviour {
public GameObject map; //Holds the map object
private MapDataA mapdataa; //stores reference to data script
public Camera mainCamera;
void Awake(){
mapdataa = map.GetComponent<MapDataA> (); //Links variable with component
}
//object for storing tile locations
public class location{
public int xCoor;
public int yCoor;
//methods rarely interact with f as it is usually ignored, I tried to keep it isolated from extra interactions and assignments
public int f;
location parent; //location the object came from in A* algorithm
public int g;
public int h;
public location(){
xCoor = 0;
yCoor = 0;
}
public location(int x, int y){
xCoor = x;
yCoor = y;
}
public location(location l){
//constructor creates a copy of the location but does not copy the F value
xCoor = l.getX ();
yCoor = l.getY ();
}
public location(int x, int y, location a, location b){ //constructor for a*
//x is x coordinate
//y is y coordinate
//a is starting position in algorithm ("from" tile)
//b is target position in algorithm ("to" tile)
xCoor = x;
yCoor = y;
//g , h , f
//g = getD(a, new location (x,y));
//h = getD(new location(x,y), b);
//f = getD(a, new location (x,y)) + getD(new location(x,y), b);
//--- seems I can not nest constructors, going to simply do the same job by not using the getD method and using the method's logic + code instead.
//private int getD(location a, location b){ //used for calculating g and h
//if G,
//a is the start point
//b is the current square
//if H,
//a is the current square
//b is the endpoint
// int total = Math.Abs (a.getY () - b.getY ()) + Math.Abs (a.getX () - b.getX ());
// return total;
//}
g = Math.Abs (a.getY () - y) + Math.Abs (a.getX () - x);
h = Math.Abs (y - b.getY ()) + Math.Abs (x - b.getX ());
f = (Math.Abs (a.getY () - y) + Math.Abs (a.getX () - x)) + (Math.Abs (y - b.getY ()) + Math.Abs (x - b.getX ()));
}
public void setX(int x){
xCoor = x;
}
public void setY(int y){
yCoor = y;
}
public int getX(){
return xCoor;
}
public int getY(){
return yCoor;
}
public void setParent(location p){
parent = p;
}
public location getParent(){
return parent;
}
public void setF(int i){
f = i;
}
public int getF(){
return f;
}
public void setG(int gset){
g = gset;
}
public int getG(){
return g;
}
public void setH(int hset){
h = hset;
}
public int getH(){
return h;
}
public bool compareTo(location c){
if ((c.getX () == this.getX ()) && (c.getY () == this.getY ())){
return true;
}else{
return false;
}
}
public string toString(){
string str = "x: " + xCoor + " y: " + yCoor;
return str;
}
}
List<location> open = new List<location> (); //A* open list
List<location> closed = new List<location> (); //A* closed list
//method for the A* pathfinding algorithm's implementation in player movement
//Helped greatly in writing this section:
//http://www.raywenderlich.com/4946/introduction-to-a-pathfinding
//http://www.raywenderlich.com/4970/how-to-implement-a-pathfinding-with-cocos2d-tutorial
public void aStar(location a, location b){
//a is starting position
//b is ending position
//checks to see if the destination is the same as the current position
if (a.compareTo (b) == true) {
Debug.Log ("Already There");
return;
}
//Checks to see if the id of the destination tile is 1, and thus innacessable
Debug.Log (mapdataa.mapData.GetLength (0));
Debug.Log (mapdataa.mapData.GetLength (1));
Debug.Log (b.getY ());
Debug.Log (b.getX ());
if (mapdataa.mapData[b.getY(), b.getX ()].getId () == 1){
Debug.Log ("Destination tile is inaccessable");
return;
}
//checks to see if the tile is highlighted (aka in range)
GameObject[] inRange = GameObject.FindGameObjectsWithTag ("moveTransparent");
bool found = false;
foreach (GameObject trans in inRange) {
if (trans.transform.position.x == b.getX () && trans.transform.position.y == b.getY ()){
//tile is within range
found = true;
}
}
if (found == false) { //This section has worked in testing.
//tile was never found to be within range
Debug.Log ("Tile not in range/Tile not highlighted");
return;
}
//begin A* actual algorithm implementation here
bool pathFound = false;
open.Add(new location ((int)transform.position.x, (int)transform.position.y, a, b)); //adds initial location of playable to open list
//testing
Debug.Log ("open[0]: " + open[0].toString ());
Debug.Log (open.Count);
//end testing
//above was previously an addToOpen method call, but I changed it to add, it SHOULD not affect logic, and may fix bugs
location current = new location ();
location temp = new location ();
//Something in here (main algorithmic loop) is causing the crash
do{
//Get the lowest F score step
//Because the list is ordered, the first step always has the lowest F score
current = open[0];
//add the current step to the closed list
closed.Add (current);
//remove the current step from the open list
open.RemoveAt (0);
//if current is the desired tile coordinate, the algorithm is complete
if(current.compareTo (b)){
pathFound = true;
temp = current;
Debug.Log ("Path Found");
//LOOK INTO THIS! PARENT CHILD STRUCTURE NEEDS TO BE CONSIDERED
do{
Debug.Log (temp);
temp = temp.getParent();
}while(temp != null);
break;
}
//get the adjacent tiles to the current step
location[] adjSteps = getAdjacent (current,a,b);
foreach (location loc in adjSteps){
//check if the step isn't already in the closed set
if (closed.Contains (loc)){
continue; //effectively ignores it
}
//check if the step is already in the open list
//NOTE: the line here in the source material is iffy, there might be functionality that I am not implementing
if (open.Contains (loc) == false){
//aka, not in the open list
//set current step as the parent
loc.setParent (current); //3
//G score is parent g score + 1 (cost to move from parent to it)
loc.setG (current.getG () + 1); //4
//compute H score
loc.setH (getD (loc, b)); //5
//adding it to the open list
addToOpen(loc);
}else{
//aka, already in the open list
int index = open.IndexOf (loc);
//loc = open[index];
//check to see if the G score is equal to the parent g score + cost to move(1)
if(current.getG () + 1 < open[index].getG ()){
//The G score is equal to the parent G score + the cost to move to it
open[index].setG (current.getG () + 1);
//because the G score changed, f may change too
//so to keep the open list ordered, we have to remove and reinsert it
location locB = new location(open[index]); //added to circumvent bug
//remove from the list
open.RemoveAt (index);
//reinsert it
addToOpen (locB); //replaced loc with locB to circumvent bug
}
}
}
}while(open.Count > 0);
if (!pathFound) { //no path found
Debug.Log ("The algorithm failed to discover a path");
}
}
private void addToOpen(location l){ //causes editor crash
int fScore = l.getF (); //f score of the location to add
int count = open.Count; //number of values in the list
int index = 0; //index to insert the location
Debug.Log ("count " + count);
while (index < count) {
if(fScore >= open[index].getF()){
break;
}
index++;
}
open.Insert(index, temp);
}
//majorly reworked to prohibit returning null values to avoid editor crash while fixing bugs
private location[] getAdjacent(location l, location a, location b){
location[] adjacents = new location[4];
GameObject[] inRange = GameObject.FindGameObjectsWithTag ("moveTransparent");
bool found = false;
int counter = 0;
//up
location up = new location (l.getX (), l.getY () + 1,a ,b);
foreach (GameObject trans in inRange) {
if (trans.transform.position.x == up.getX () && trans.transform.position.y == up.getY ()){
//tile is within range
found = true;
}
}
if ((found == true) && (mapdataa.mapData [up.getY (), up.getX ()].getId () == 0)) { //if tile is within range and is acessable
adjacents [counter] = up; //add it to the list
counter++;
}
found = false; //reset found variable
//down
location down = new location (l.getX (), l.getY () -1,a,b);
foreach (GameObject trans in inRange) {
if (trans.transform.position.x == down.getX () && trans.transform.position.y == down.getY ()){
//tile is within range
found = true;
}
}
if ((found == true) && (mapdataa.mapData [down.getY (), down.getX ()].getId () == 0)) { //if tile is within range and is acessable
adjacents [counter] = down; //add it to the list
counter++;
}
found = false; //reset found variable
//left
location left = new location (l.getX () -1, l.getY (),a,b);
foreach (GameObject trans in inRange) {
if (trans.transform.position.x == left.getX () && trans.transform.position.y == left.getY ()){
//tile is within range
found = true;
}
}
if ((found == true) && (mapdataa.mapData [left.getY (), left.getX ()].getId () == 0)) { //if tile is within range and is acessable
adjacents [counter] = left; //add it to the list
counter++;
}
found = false; //reset found variable
//right
location right = new location (l.getX () +1, l.getY (),a,b);
foreach (GameObject trans in inRange) {
if (trans.transform.position.x == right.getX () && trans.transform.position.y == right.getY ()){
//tile is within range
found = true;
}
}
if ((found == true) && (mapdataa.mapData [right.getY (), right.getX ()].getId () == 0)) { //if tile is within range and is acessable
adjacents [counter] = right; //add it to the list
counter++;
}
found = false; //reset found variable
if (counter < 4) { //if statement is logically useless, but for the sake of trying to get to work, I'm leaving it as is. Once it runs, try removing the if statement and test again.
adjacents = retract (adjacents, 4-counter);
}
return adjacents;
}
private int getD(location a, location b){ //used for calculating g and h
//implements the "Manhatten method" of determining total vertical and horizontal distance on a grid
//if G,
//a is the start point
//b is the current square
//if H,
//a is the current square
//b is the endpoint
int total = Math.Abs (a.getY () - b.getY ()) + Math.Abs (a.getX () - b.getX ());
return total;
}
bool ran = false;
public void Update(){
if (ran == false) {
highlight ();
ran = true;
}
if (Input.GetMouseButtonDown (0)) { //if left click
Debug.Log ("Begin Update method A* implementation testing");
aStar (new location((int)transform.position.x,(int)transform.position.y),new location((int)((((mainCamera.transform.position.x * 64) - (.5 * Screen.width)) + Input.mousePosition.x) / 64),(int)((((mainCamera.transform.position.y * 64) - (.5 * Screen.height)) + Input.mousePosition.y) / 64)));
Debug.Log ("End Update method A* implementation testing");
}
}
最佳答案
我无法真正调试它,因为我没有完整的上下文,但这是我查看源代码的猜测
//This line seems to cause the editor crash
open.Insert (index, l);
open
: 存储元素的列表(计数 可能 为零)index
: 一个索引,从 1 开始(如果 open.Count==0,则保持在 1 )int a=1/0
或类似的测试)。如果没有,很可能您在 MonoDevelop 中发现了一个错误(在这种情况下,您应该提交错误报告)。当然,也可能是其他原因导致崩溃(例如算法中的无限循环)。
关于C# 脚本导致 Unity 编辑器崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24834061/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!