1
我收到一個異常,絕對沒有任何意義。我試圖用這個深度優先算法進行隨機迷宮:InvalidOperationException當試圖使用Stack.Pop()
http://www.mazeworks.com/mazegen/mazetut/
我最一切實現,但是當我運行該程序,我得到這個錯誤:
InvalidOperationException: Operation is not valid due to the current state of the object System.Collections.Generic.Stack 1[UnityEngine.GameObject].Pop()
不幸的是堆棧沒有顯示在檢查器中,所以我無法查看堆棧的「當前狀態」。
使用調試器時也不會發生此錯誤。
這裏是我的代碼:
/***************************************************************************************/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class MazeGenerator : MonoBehaviour
{
public GameObject[] m_totalCells; // all the cells in the level
public GameObject[] m_walls; // all the walls in the level
public List<GameObject> m_visited; // list of each visited cell
public Stack<GameObject> m_cellStack; // last-in, first-out stack for generating the maze
public GameObject m_currentCell; // the cell currently being looked at
public int m_visitedCells = 0; // how many cells have been looked at
public bool m_puzzleCreated; // has the puzzle been made yet
/*****************************************************************************/
/*
Description:
Use this for initialization.
Parameters:
- none
Return:
- none
*/
/*****************************************************************************/
void Start()
{
m_totalCells = GameObject.FindGameObjectsWithTag("Cell");
m_walls = GameObject.FindGameObjectsWithTag("Wall");
//initialize the stack
m_visited = new List<GameObject>();
}
/*****************************************************************************/
/*
Description:
Make a random maze using the depth-field algorithm.
Parameters:
- none
Return:
- none
*/
/*****************************************************************************/
void GeneratePuzzle()
{
// make sure all the cells have found their neighbors and walls
foreach(GameObject cell in m_totalCells)
{
cell.GetComponent<Cell>().FindAdjacents();
cell.GetComponent<Cell>().FindWalls();
cell.GetComponent<Cell>().FindNeighbors();
}
m_cellStack = new Stack<GameObject>();
// grab a randon cell
int randomizer = Random.Range(0, m_totalCells.Length);
m_currentCell = m_totalCells[randomizer];
m_visitedCells = 1;
while(m_visitedCells < m_totalCells.Length)
{
m_visited.Add(m_currentCell);
if(m_totalCells[randomizer].GetComponent<Cell>().FindNeighbors() == true)
{
m_cellStack.Push(m_currentCell);
m_currentCell = m_currentCell.GetComponent<Cell>().BreakWall();
m_visitedCells++;
}
else
{
/*********************************/
/* This is where I Pop() */
/*********************************/
m_currentCell = m_cellStack.Pop();
}
}
foreach(GameObject wall in m_walls)
{
if(wall.gameObject.GetComponent<Wall>().m_destroyed == true)
{
Destroy(wall);
}
}
}
/*****************************************************************************/
/*
Description:
Update is called once a frame.
Parameters:
- none
Return:
- none
*/
/*****************************************************************************/
void Update()
{
if(Input.GetMouseButtonDown(0) && m_puzzleCreated == false)
{
m_puzzleCreated = true;
GeneratePuzzle();
}
}
}