- Home /
How to Destroy items in Match 3.
hello, i am trying to create a match three game, i am stuck in process of destroying the object when it has 3 items of same tag in row or column, i have tried many methods but everything seems to fail...
The MainObject has 4 child colliders on four sides to check the other objects tag...do u hv any helpful suggestion to make it work?
$$anonymous$$y suggestion: forget all about physics. Given the size of a level in a match 3 game, you can keep everything inside a logical representation (you even have rows and columns). Whenever you get to a stable situation (i.e. no objects moving/falling) check each object for its neighbours, one at a time. While you are checking one object keep track of the number of matching neighbors, if more than 3 mark them as "destroy". Rince and repeat.
[edit:] You can improve this by tracking the turn and check only for the objects that have moved in the last turn.
@andres i read your comment late, i used physics to check the neighbours, well you can say it works 90% fine and in some instances it screws up and destroys when it should, can u brief me more on how to not use physics and how to assign them positions,give gravity and check neighbours?
@koray1396 i created the tiles, made tiles swap, using multidimensional array,but how do i make them check for match all the time?
I would wait for a stable moment/turn (it can be when all objects have stopped) and then assign each a logical row and column based on its transform.position.
If you are just checking for rows and columns, like if the below will not make any match;
000
112
123
You can check for combinations at the end of every turn as Andres suggested. Not tested but my method would be something like that, sorry if there are any errors, wrote this in a hassle.
a better way would be just to check changed rows and columns. But this would be easier if you have combos or anything...
I would also create a struct for storing tile coordinates easily.
public struct TileCoord{
public int Column, Row;
public TileCoord(int x, int y){
Column = x;
Row = y;
}
}
void CheckForCombinations(int colNr, int rowNr){
//Check Columns For Combinations
List<TileCoord> TilesToDestroy = new List<TileCoord>();
int startRowNumber = 0;
int numberOfTiles$$anonymous$$atched = 0;
for(int i = 0; i < NumberOfColumns; i++){
numberOfTiles$$anonymous$$atched = 0;
for(int y = 0; y < NumberOfRows - 1; y++){
if(tileTypes[i,y] != 0 && tileTypes[i,y] == tileTypes[i,y+1]){
numberOfTiles$$anonymous$$atched += 1;
if(numberOfTiles$$anonymous$$atched == 1){
startRowNumber = y;
}
} else {
if(numberOfTiles$$anonymous$$atched >= 2){
for(int z = 0; z <= numberOfTiles$$anonymous$$atched; z++){
TilesToDestroy.Add(new TileCoord(i, startRowNumber + z));
}
}
numberOfTiles$$anonymous$$atched = 0;
}
}
if(numberOfTiles$$anonymous$$atched >= 2){
for(int z = 0; z <= numberOfTiles$$anonymous$$atched; z++){
TilesToDestroy.Add(new TileCoord(i, startRowNumber + z));
}
}
//Check Rows Similar To Above
//Destroy tiles in list
}
otherwise if tiles can make a match as below;
110
122
344
Then you would need a a more complex way to check combinations, I would check neighbours of newly added tiles, and neighbours of neighbours of newly added (or changed) tiles.
Answer by koray1396 · Nov 28, 2014 at 11:59 AM
use multidimensional array to store tile types, such as;
int[,] tileTypes = new int[NumberOfColumns, NumberOfRows]; //0 would be empty tile. 1,2,3... would represent types.
Can you please send me the code regarding this(how to destroy gems if the colour matches) $$anonymous$$y email is sitanshyadav@gmail.com Please.........
Answer by nirharpaz · Dec 02, 2014 at 05:12 PM
did you try the function Destroy(obj:Object, t:float) ?