Search code examples
c#.netxnaxna-4.0

Swapping tiles in a Match3 game


I'm a newbie here and in XNA programming, but I have some skills in C#. Now I am trying to make a match3 game in XNA 4.0 with Visual Studio 2010 and I got into trouble.

I have created a class, called Tile, which has information about the tile like: Texture2D, Vector2 position, some bools, ints and so on. In another class I have 2D array (8x8) of this tile objects (total of 64). When I run the game I can see the grid with the textures as they should be. I was able to create a method for selecting the tiles by mouse clicking, but I can't swap the two tiles. Here is my swap method:

public void swapPieces(int x1, int y1, int x2, int y2)
{
    Tile temp;

    temp = Tiles[x1, y1];
    Tiles[x1, y1] = Tiles[x2, y2];
    Tiles[x2, y2] = temp;
}

where x1, y1, x2 and y2 are the coordinates in the array of the two selected tiles and Tiles[,] is my two dimensional array. When I trace the game, I see that the tiles are swapped, but visual I see no changes. How I can deal with this situation? Thanks.


Solution

  • Your code for the swapping looks right.. Atm you have 2 separate systems for the tiles position. The positing vector and the array...either you need to both swap the vector and the location in the array or throw out one of the position systems.