Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2048 Moving the tiles

Discussion in 'Scripting' started by White8eard, Dec 9, 2014.

  1. White8eard

    White8eard

    Joined:
    Aug 4, 2014
    Posts:
    61
    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 https://github.com/jamiltron/2048-unity as reference, i am new to using arrays and multidimensional arrays...any help would be appreciated.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.  
    6.     public GameObject tile;
    7.     public GameObject objs;
    8.     public int GridX,GridY;
    9.     public int[,] Area;
    10.     public int currerntTileAmount = 0;
    11.     private static float spaceBetweenTiles = 1f;
    12.     private static Vector3 horizontalRay = new Vector3(1f, 0f, 0f);
    13.     private static Vector3 verticalRay = new Vector3(0f, 1f, 0f);
    14.    
    15.     // Use this for initialization
    16.     void Start () {
    17.         Area = new int[GridX,GridY];
    18.         for(int x = 0;x<GridX;x++)
    19.         {
    20.             for(int y = 0;y<GridY;y++)
    21.             {
    22.                 GameObject go = Instantiate(tile,new Vector3(x,y,0),Quaternion.identity) as GameObject;
    23.                 //go.gameObject.name = "Orb";
    24.                 go.transform.parent = this.transform;
    25.                 //Area.Add(go);
    26.             }
    27.         }
    28.         gameObject.transform.position = new Vector3(0f,0f,0f);
    29.  
    30.         generateRandomTiles();
    31.         generateRandomTiles();
    32.     }
    33.    
    34.     // Update is called once per frame
    35.     void Update () {
    36.         if(Input.GetKeyDown(KeyCode.LeftArrow))
    37.         {
    38.             MoveTilesLeft();
    39.         }
    40.     }
    41.  
    42.     private static Vector2 GridToWorldPoint(int x, int y) {
    43. //        return new Vector2(x + horizontalSpacingOffset + borderSpacing * x,
    44. //                           -y + verticalSpacingOffset - borderSpacing * y);
    45.         return new Vector2(x,y);
    46.     }
    47.    
    48.     private static Vector2 WorldToGridPoint(float x, float y) {
    49. //        return new Vector2((x - horizontalSpacingOffset) / (1 + borderSpacing),
    50. //                           (y - verticalSpacingOffset)   / -(1 + borderSpacing));
    51.         return new Vector2(x,y);
    52.     }
    53.  
    54.     private void UpdateGrid(GameObject currentTile, Vector2 amountToMove) {
    55.         Transform tileTransform = currentTile.transform;
    56.         Vector2 gridPoint = WorldToGridPoint(tileTransform.position.x, tileTransform.position.y);
    57.         Area[Mathf.RoundToInt(gridPoint.x),Mathf.RoundToInt(gridPoint.y)] = 0;
    58.         //Area[Mathf.RoundToInt(currentTile.transform.position.x),Mathf.RoundToInt(currentTile.transform.position.y)] = 0;
    59.        
    60.         Vector2 newPosition = currentTile.transform.position;
    61.         newPosition += amountToMove;
    62.         currentTile.transform.position = newPosition;
    63.        
    64.         gridPoint = WorldToGridPoint(tileTransform.position.x, tileTransform.position.y);
    65.         Tile tile = currentTile.GetComponent<Tile>();
    66.         Area[Mathf.RoundToInt(gridPoint.x),Mathf.RoundToInt(gridPoint.y)] = tile.id;
    67.         //Area[Mathf.RoundToInt(currentTile.transform.position.x),Mathf.RoundToInt(currentTile.transform.position.y)] = tile.id;
    68.     }
    69.    
    70.     void generateRandomTiles()
    71.     {
    72.         if(currerntTileAmount> GridX * GridY)
    73.         {
    74.             throw new UnityException("Grids are full");
    75.         }
    76.         int id = Random.Range(1,5);
    77.         Debug.Log("ID: "+id);
    78.         int x = Random.Range(0,GridX);
    79.         Debug.Log("X: "+x);
    80.         int y = Random.Range(0,GridY);
    81.         Debug.Log("Y: "+y);
    82.         bool found = false;
    83.         while(!found)
    84.         {
    85.             if(Area[x,y] == 0)
    86.             {
    87.                 found = true;
    88.                 Area[x,y] = id;
    89.                 Vector2 worldposition = GridToWorldPoint(x,y);
    90.                 GameObject obj;
    91.                 obj = Instantiate(objs,worldposition,transform.rotation) as GameObject;
    92.                 obj.GetComponent<Tile>().x = x;
    93.                 obj.GetComponent<Tile>().y = y;
    94.                 obj.GetComponent<Tile>().id = id;
    95.                 currerntTileAmount++;
    96.  
    97.             }
    98.  
    99.             x++;
    100.             if (x >= GridY) {
    101.                 y++;
    102.                 x = 0;
    103.             }
    104.            
    105.             if (y >= GridX) {
    106.                 y = 0;
    107.             }
    108.            
    109.         }
    110.        
    111.     }
    112.  
    113.     private bool MoveTilesLeft() {
    114.         bool hasMoved = false;
    115.         for (int x = 1; x < 6; x++) {
    116.             for (int y = 5; y >= 0; y--) {
    117.                 if (Area[x, y] == 0) {
    118.                     continue;
    119.                 }
    120.                
    121.                 GameObject currentTile = GetObjectAtGridPosition(x, y);
    122.                 bool stopped = false;
    123.                
    124.                 while (!stopped) {
    125.                     // see if the position to the left is open
    126.                     RaycastHit2D hit = Physics2D.Raycast (currentTile.transform.position - horizontalRay, -Vector2.right);
    127.                     if (hit && hit.collider.gameObject != currentTile) {
    128.                         Debug.Log("Moving");
    129.                         UpdateGrid (currentTile, new Vector2(-spaceBetweenTiles, 0f));
    130.                         hasMoved = true;
    131. //                        Tile otherTile = hit.collider.gameObject.GetComponent<Tile>();
    132. //                        if (otherTile != null) {
    133. //                            Tile thisTile = currentTile.GetComponent<Tile>();
    134. //                            //              if (thisTile.power == otherTile.power) {
    135. //                            //                UpgradeTile(currentTile, thisTile, hit.collider.gameObject, otherTile);
    136. //                            //                hasMoved = true;
    137. //                            //              }
    138. //                        }
    139. //                        stopped = true;
    140.                     } else {
    141.                         Debug.Log("Moving");
    142.                         UpdateGrid (currentTile, new Vector2(-spaceBetweenTiles, 0f));
    143.                         hasMoved = true;
    144.                     }
    145.                 }
    146.             }
    147.         }
    148.         return hasMoved;
    149.     }
    150.  
    151.     void OnGUI()
    152.     {
    153.         if(GUI.Button(new Rect(Screen.width * 0.8f,Screen.height * 0.1f,Screen.width * 0.1f,Screen.height * 0.1f),"Generate"))
    154.         {
    155.             generateRandomTiles();
    156.         }
    157.     }
    158.  
    159.  
    160.     private GameObject GetObjectAtGridPosition(int x, int y) {
    161.         RaycastHit2D hit = Physics2D.Raycast (GridToWorldPoint (x, y), Vector2.right);
    162.        
    163.         if (hit) {
    164.             Debug.Log(hit.collider.name);
    165.             return hit.collider.gameObject;
    166.         }
    167.         else {
    168.             throw new UnityException("Unable to find gameObject in grid position (" + x + ", " + y + ")");
    169.         }
    170.     }
    171. }
    172.  
     
  2. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    WorldToGridPoint is returning something that is out of bounds of your 2d array. That could mean that your tile positions aren't perfectly in line with your ideal grid, or that your GridX and GridY values aren't set up right (you hardcode a dimension of 6 in MoveTilesLeft, but your grid is generated dynamically based on the parameter).
     
  3. White8eard

    White8eard

    Joined:
    Aug 4, 2014
    Posts:
    61
    made changes here

    Code (CSharp):
    1. private GameObject GetObjectAtGridPosition(int x, int y) {
    2.         RaycastHit2D hit = Physics2D.Raycast (GridToWorldPoint (x, y), Vector2.up,.1f);
    3.        
    4.         if (hit) {
    5.             Debug.Log(hit.collider.name);
    6.             Debug.Log(hit.collider.transform.position.x+" "+hit.collider.transform.position.y);
    7.             return hit.collider.gameObject;
    8.         }
    9.         else {
    10.             throw new UnityException("Unable to find gameObject in grid position (" + x + ", " + y + ")");
    11.         }
    12.     }
    and i received this error, the problem was all this time it was hitting the border colliders and not the tile object.