Hello,i am working on 2048 game, here i have made a 6,6 multidimensional array, i am trying to make the the tiles move to left, but i am getting this error
i took GitHub - jamiltron/2048-unity: A 2048 clone written in an afternoon to see how easy it would be to do in Unity as reference, i am new to using arrays and multidimensional arrays…any help would be appreciated.
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
public GameObject tile;
public GameObject objs;
public int GridX,GridY;
public int[,] Area;
public int currerntTileAmount = 0;
private static float spaceBetweenTiles = 1f;
private static Vector3 horizontalRay = new Vector3(1f, 0f, 0f);
private static Vector3 verticalRay = new Vector3(0f, 1f, 0f);
// Use this for initialization
void Start () {
Area = new int[GridX,GridY];
for(int x = 0;x<GridX;x++)
{
for(int y = 0;y<GridY;y++)
{
GameObject go = Instantiate(tile,new Vector3(x,y,0),Quaternion.identity) as GameObject;
//go.gameObject.name = "Orb";
go.transform.parent = this.transform;
//Area.Add(go);
}
}
gameObject.transform.position = new Vector3(0f,0f,0f);
generateRandomTiles();
generateRandomTiles();
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
MoveTilesLeft();
}
}
private static Vector2 GridToWorldPoint(int x, int y) {
// return new Vector2(x + horizontalSpacingOffset + borderSpacing * x,
// -y + verticalSpacingOffset - borderSpacing * y);
return new Vector2(x,y);
}
private static Vector2 WorldToGridPoint(float x, float y) {
// return new Vector2((x - horizontalSpacingOffset) / (1 + borderSpacing),
// (y - verticalSpacingOffset) / -(1 + borderSpacing));
return new Vector2(x,y);
}
private void UpdateGrid(GameObject currentTile, Vector2 amountToMove) {
Transform tileTransform = currentTile.transform;
Vector2 gridPoint = WorldToGridPoint(tileTransform.position.x, tileTransform.position.y);
Area[Mathf.RoundToInt(gridPoint.x),Mathf.RoundToInt(gridPoint.y)] = 0;
//Area[Mathf.RoundToInt(currentTile.transform.position.x),Mathf.RoundToInt(currentTile.transform.position.y)] = 0;
Vector2 newPosition = currentTile.transform.position;
newPosition += amountToMove;
currentTile.transform.position = newPosition;
gridPoint = WorldToGridPoint(tileTransform.position.x, tileTransform.position.y);
Tile tile = currentTile.GetComponent<Tile>();
Area[Mathf.RoundToInt(gridPoint.x),Mathf.RoundToInt(gridPoint.y)] = tile.id;
//Area[Mathf.RoundToInt(currentTile.transform.position.x),Mathf.RoundToInt(currentTile.transform.position.y)] = tile.id;
}
void generateRandomTiles()
{
if(currerntTileAmount> GridX * GridY)
{
throw new UnityException("Grids are full");
}
int id = Random.Range(1,5);
Debug.Log("ID: "+id);
int x = Random.Range(0,GridX);
Debug.Log("X: "+x);
int y = Random.Range(0,GridY);
Debug.Log("Y: "+y);
bool found = false;
while(!found)
{
if(Area[x,y] == 0)
{
found = true;
Area[x,y] = id;
Vector2 worldposition = GridToWorldPoint(x,y);
GameObject obj;
obj = Instantiate(objs,worldposition,transform.rotation) as GameObject;
obj.GetComponent<Tile>().x = x;
obj.GetComponent<Tile>().y = y;
obj.GetComponent<Tile>().id = id;
currerntTileAmount++;
}
x++;
if (x >= GridY) {
y++;
x = 0;
}
if (y >= GridX) {
y = 0;
}
}
}
private bool MoveTilesLeft() {
bool hasMoved = false;
for (int x = 1; x < 6; x++) {
for (int y = 5; y >= 0; y--) {
if (Area[x, y] == 0) {
continue;
}
GameObject currentTile = GetObjectAtGridPosition(x, y);
bool stopped = false;
while (!stopped) {
// see if the position to the left is open
RaycastHit2D hit = Physics2D.Raycast (currentTile.transform.position - horizontalRay, -Vector2.right);
if (hit && hit.collider.gameObject != currentTile) {
Debug.Log("Moving");
UpdateGrid (currentTile, new Vector2(-spaceBetweenTiles, 0f));
hasMoved = true;
// Tile otherTile = hit.collider.gameObject.GetComponent<Tile>();
// if (otherTile != null) {
// Tile thisTile = currentTile.GetComponent<Tile>();
// // if (thisTile.power == otherTile.power) {
// // UpgradeTile(currentTile, thisTile, hit.collider.gameObject, otherTile);
// // hasMoved = true;
// // }
// }
// stopped = true;
} else {
Debug.Log("Moving");
UpdateGrid (currentTile, new Vector2(-spaceBetweenTiles, 0f));
hasMoved = true;
}
}
}
}
return hasMoved;
}
void OnGUI()
{
if(GUI.Button(new Rect(Screen.width * 0.8f,Screen.height * 0.1f,Screen.width * 0.1f,Screen.height * 0.1f),"Generate"))
{
generateRandomTiles();
}
}
private GameObject GetObjectAtGridPosition(int x, int y) {
RaycastHit2D hit = Physics2D.Raycast (GridToWorldPoint (x, y), Vector2.right);
if (hit) {
Debug.Log(hit.collider.name);
return hit.collider.gameObject;
}
else {
throw new UnityException("Unable to find gameObject in grid position (" + x + ", " + y + ")");
}
}
}