首先发布在这里。
我自我教导C#和团结,并在#34; match-3"风格游戏类似于宝石迷阵。我已根据教程编写了下面发布的所有代码,但是我自己和评论部分中的其他人都遇到了阵列超出范围的问题。
编译器说的错误。
IndexOutOfRangeException: Array index is out of range.
CreateGame.CheckGrid () (at Assets/Scripts/CreateGame.cs:181)
CreateGame.Update () (at Assets/Scripts/CreateGame.cs:229)
下面粘贴了CreateGame.cs的完整代码(整个项目中唯一的脚本)。但是为了帮助你,我可以说,根据我研究的结果,当你搜索没有存储在数组中的东西时,就会出现索引。作为C#的新手,只有2个项目(也来自教程)我缺乏如何真正解决这个问题的技术知识。
根据我的理解,当数组小于0时(例如负值),编译器将返回此错误,因为-1显然超出范围。
我认为罪魁祸首与这些if语句有关,特别是我们从c整数中减去1的情况。
if (tiles[c, r] != null && tiles[c - 1, r] != null)
但是上面的内容嵌套在for语句
中for (int c = 0; c < cols; c++)
所以C的值永远不应该小于零,否则我误解了这个for语句的逻辑,c从0开始,然后如果它小于cols变量则加1,或者当c = 0时,它首先完成for循环。我不确定如何解决这个问题。
另外值得一提的是,数组大小是在我正在工作的场景内的GameObject(在检查器中)中定义的,为4,并且我有4个单独的对象预制件分配给tile数组的4个值,并将它们的位置存储在切片数组中(如代码中所示)。此外,cs脚本仅附加到单个游戏对象,因此没有多个代码实例在运行。
对不起,如果这个组织得不好或者其他什么,我已经学习了一个星期。
感谢您的期待! :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tile
{
public GameObject tileObj;
public string type;
public Tile(GameObject obj, string t)
{
tileObj = obj;
type = t;
}
}
public class CreateGame : MonoBehaviour
{
GameObject tile1 = null;
GameObject tile2 = null;
public GameObject[] tile;
List<GameObject> tileBank = new List<GameObject> ();
static int rows = 8;
static int cols = 8;
bool renewBoard = false;
Tile[,] tiles = new Tile[cols, rows];
void Start()
{
//List population
int numCopies = (rows * cols) / 3;
for (int i = 0; i < numCopies; i++)
{
for(int j = 0; j < tile.Length; j++) // J becomes the int = to number of tile prefabs added
{
GameObject o = (GameObject)Instantiate
(tile [j], new Vector3 (-10, -10, 0), tile [j].transform.rotation); // creates a clone tile of the screen
o.SetActive (false);
tileBank.Add (o); // adds tile (game object called o) to the array (not shuffled)
}
}
ShuffleList(); //Shuffles the list of tile-bank after the list has been populated
//Populates rows and columns with tiles from tileBank array
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
Vector3 tilePos = new Vector3 (c - 2, r - 4, 0); //creates tiles with an offset ( -2x and -4y) to place them in scene
for (int n = 0; n < tileBank.Count; n++)
{
GameObject o = tileBank [n];
if (!o.activeSelf) // if the tile is not active move it and make it active
{
o.transform.position = new Vector3 (tilePos.x, tilePos.y, tilePos.z); //moves tile into position, it was stored inactive at -10,-10
o.SetActive (true);
tiles[c, r] = new Tile (o, o.name); //Stores tile back into array
n = tileBank.Count + 1; //Update tile count
}
}
}
}
}
//Shuffles the order of the array storing the tiles
void ShuffleList()
{
System.Random rand = new System.Random ();
int r = tileBank.Count;
while (r > 1)
{
r--;
int n = rand.Next (r + 1);
GameObject val = tileBank [n];
tileBank [n] = tileBank [r];
tileBank [r] = val;
}
}
//checks for missing tiles and adds new ones from array, also drops tiles down if there is an opening
void RenewGrid()
{
bool anyMoved = false;
ShuffleList ();
for (int r = 1; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
if (r== rows - 1 && tiles[c, r] == null)
//if in the top row and there are no tiles
{
Vector3 tilePos = new Vector3 (c, r, 0);
for (int n = 0; n < tileBank.Count; n++)
{
GameObject o = tileBank [n];
if (!o.activeSelf)
{
o.transform.position = new Vector3 (tilePos.x, tilePos.y, tilePos.z);
o.SetActive (true);
tiles [c, r] = new Tile (o, o.name);
n = tileBank.Count + 1;
}
}
}
if (tiles[c, r - 1] != null)
{
if (tiles[c, r - 1] == null)
//drop down if space below is empty
{
tiles [c, r - 1] = tiles [c, r];
tiles [c, r - 1].tileObj.transform.position = new Vector3 (c, r - 1, 0);
tiles [c, r] = null;
anyMoved = true;
}
}
}
}
if (anyMoved)
{
Invoke ("RenewGrid", 0.5f);
}
}
void CheckGrid()
{
int counter = 1;
//check in columns for matches
for (int r = 0; r < rows; r++) //repeats function until you have satisfied the number of rows
{
counter = 1;
for (int c = 1; c < cols; c++)
{
if (tiles[c, r] != null && tiles[c - 1, r] != null) //if the tiles exist (not null)
{
if (tiles[c, r].type == tiles [c - 1, r].type) //if there is a match add 1 to the counter for match checks
{
counter++;
}
else
{
counter = 1; //reset counter
}
//if a match is found remove tiles
if (counter == 3)
{
if (tiles [c, r] != null)
{
tiles [c, r].tileObj.SetActive(false);
}
if (tiles [c - 1, r] !=null)
{
tiles [c - 1, r].tileObj.SetActive(false);
}
if (tiles [c - 2, r] !=null)
{
tiles [c - 2, r].tileObj.SetActive(false);
}
tiles[c , r] = null; // changes values to null in the 2D matrix
tiles[c - 1, r] = null;
tiles[c - 2, r] = null;
renewBoard = true;
}
}
}
}
//check in rows for matches
for (int c = 0; c < cols; c++)
{
counter = 1;
for (int r = 1; c < rows; r++)
{
if (tiles[c, r] != null && tiles[c - 1, r] != null) //if the tiles exist (not null)
{
if (tiles[c, r].type == tiles [c - 1, r].type) //if there is a match add 1 to the counter for match checks
{
counter++;
}
else
{
counter = 1; //reset counter
}
//if a match is found remove tiles
if (counter == 3)
{
if (tiles [c, r] != null)
{
tiles [c, r].tileObj.SetActive(false);
}
if (tiles [c, r - 1] !=null)
{
tiles [c, r - 1].tileObj.SetActive(false);
}
if (tiles [c, r - 2] !=null)
{
tiles [c, r - 2].tileObj.SetActive(false);
}
tiles[c, r] = null; // changes values to null in the 2D matrix
tiles[c, r - 1] = null;
tiles[c, r - 2] = null;
renewBoard = true;
}
}
}
}
if(renewBoard)
{
RenewGrid();
renewBoard = false;
}
}
void Update()
{
CheckGrid ();
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); //casts a ray from mouse position
RaycastHit2D hit = Physics2D.GetRayIntersection (ray, 1000); //tests for ray hit
if (hit)
{
tile1 = hit.collider.gameObject; //changes tile1 null to the gameObject who's collider was hit
}
}
// if mouse button up detected after an initial tile has been hit with ray
else if (Input.GetMouseButtonUp(0) && tile1)
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit2D hit = Physics2D.GetRayIntersection (ray, 1000);
if (hit)
{
tile2 = hit.collider.gameObject; //changes tile2 nulle to the gameObject who's collider was hit
}
if (tile1 && tile2) //If we has two tile values clicked, swap them
{
//check to see that distance of clicked tiles is not greater than 1 tile
int horzDist = (int)Mathf.Abs (tile1.transform.position.x - tile2.transform.position.x);
int vertDist = (int)Mathf.Abs (tile1.transform.position.y - tile2.transform.position.y);
if (horzDist == 1 ^ vertDist == 1) // X-OR Statment, IF 1 , OR 1, but NOT BOTH
{
//get tile 1's array position
Tile temp = tiles [(int)tile1.transform.position.x, (int)tile1.transform.position.y];
//change tile 1's array position to now be tile 2's array position
tiles [(int)tile1.transform.position.x, (int)tile1.transform.position.y] =
tiles [(int)tile2.transform.position.x, (int)tile2.transform.position.y];
//change tile 2's array position to now be tile 1's old array position
tiles [(int)tile2.transform.position.x, (int)tile2.transform.position.y] = temp;
Vector3 tempPos = tile1.transform.position; //get tile 1's GameObject position
tile1.transform.position = tile2.transform.position; //change tile 1 to tile 2's GameObject position
tile2.transform.position = tempPos; //change tile 2 to tile 1's GameObject position
//reset clicked tiles
tile1 = null;
tile2 = null;
}
else
{
GetComponent<AudioSource> ().Play (); // Play error sound when illegal swap attempted
}
}
}
}
}
答案 0 :(得分:1)
错误只是在for语句中使用了不正确的变量。
for (int r = 1; c < rows; r++)
本来应该是for (int r = 1; r < rows; r++)