- Home /
match3 reorganize rematching
i am making game like candy-crush etc style. i got success in making it to swap,to match3, to reorganize but fail in rematch3. the game will go idle (hung / crash, don't know). anyone can help?
in rematch3, i use class to contain array of same type item that pose near to each other. then i use array that contain that class to hold all item. the class itself is wrote in a script below another class. this is the class :
public class PoolGroup{
public string[] baris_membername;
public string[] kolom_membername;
public string StartBlockName;
public PoolGroup(string s) {
StartBlockName = s;
baris_membername = new string[0];
kolom_membername = new string[0];
SearchSameMember();
}
public void AddMember(string s,bool barisataukolom) {
if (barisataukolom)
{
//kalau baris
System.Array.Resize<string>(ref baris_membername, baris_membername.Length + 1);
baris_membername[baris_membername.Length
- 1] = s;
}
else {
//kalau kolom
System.Array.Resize<string>(ref kolom_membername, kolom_membername.Length + 1);
kolom_membername[kolom_membername.Length
- 1] = s;
}
}
public bool IfMatch3(){
if (baris_membername.Length >=2) {return true;} else if (kolom_membername.Length >= 2) {return true;} else {return false;}
}
public bool IfExistMemberName(string s) {
if (StartBlockName==s){return true;}
if (baris_membername.Length>=1){
for (int i=0;i<=(baris_membername.Length-1);i++) {if (baris_membername[i]==s) {return true;}}
}
if (kolom_membername.Length>=1) {
for(int i=0;i<=(kolom_membername.Length-1);i++) {if(kolom_membername[i]==s){return true;}}
}
return false;
}
public void SearchSameMember() {
int endbaris=GlobalVarScript.GetBoardSize-1;
int endkolom=GlobalVarScript.GetBoardSize-1;
int baris = GameObject.Find(StartBlockName).GetComponentInChildren<BlocksUnitScript>().myPosisiX;
int kolom = GameObject.Find(StartBlockName).GetComponentInChildren<BlocksUnitScript>().myPosisiY;
int jenisimage = GameObject.Find(StartBlockName).GetComponentInChildren<BlocksUnitScript>().jenisunit;
//check to the right
int i = kolom + 1;
while (i <= endkolom)
{
GameObject gameobj = GameObject.Find("blocks[" + baris + "," + i + "]");
if (gameobj != null)
{
if (gameobj.GetComponentInChildren<BlocksUnitScript>().jenisunit
== jenisimage)
{
AddMember(gameobj.name,false);
}
else
{
i = endkolom;
}
}
i++;
}
//check to the left
i = kolom - 1;
while (i >= 0)
{
GameObject gameobj = GameObject.Find("blocks[" + baris + "," + i + "]");
if (gameobj != null)
{
if (gameobj.GetComponentInChildren<BlocksUnitScript>().jenisunit
== jenisimage)
{
AddMember(gameobj.name, false);
}
else
{
i = 0;
}
}
i--;
}
//check to the top
i = baris - 1;
while (i >= 0)
{
GameObject gameobj = GameObject.Find("blocks[" + i + "," + kolom + "]");
if (gameobj != null)
{
if (gameobj.GetComponentInChildren<BlocksUnitScript>().jenisunit
== jenisimage)
{
AddMember(gameobj.name,true);
}
else
{
i = 0;
}
}
i--;
}
//check to the bottom
i = baris + 1;
while (i <= endbaris)
{
GameObject gameobj = GameObject.Find("blocks[" + i + "," + kolom + "]");
if (gameobj != null)
{
if (gameobj.GetComponentInChildren<BlocksUnitScript>().jenisunit
== jenisimage)
{
AddMember(gameobj.name,true);
}
else
{
i = endbaris;
}
}
i++;
}
}
public void ProcessMemberMatch() {
GlobalVarScript.SetNullToBlock(StartBlockName);
int nilai_unit = GameObject.Find(StartBlockName).GetComponentInChildren<BlocksUnitScript>().nilai;
int countitem=1;
int i=0;
if (baris_membername.Length >=1) {
for (i = 0; i <= (baris_membername.Length - 1); i++) {
GlobalVarScript.SetNullToBlock(baris_membername[i]);
GlobalVarScript.DisplayPts(++countitem, nilai_unit, baris_membername[i]);
}
}
if (kolom_membername.Length >= 1)
{
for (i = 0; i <= (kolom_membername.Length - 1); i++)
{
GlobalVarScript.SetNullToBlock(kolom_membername[i]);
GlobalVarScript.DisplayPts(++countitem, nilai_unit, kolom_membername[i]);
}
}
}
}
then this is the method in another class (in same script) that do rematch3 :
public static void CheckMatchAll(){
int endPosKolom = GlobalVarScript.GetBoardSize - 1, endPosBaris = GlobalVarScript.GetBoardSize - 1;
PoolGroup[] mPoolGroup = new PoolGroup[0];
for (int i = 0; i <= endPosBaris; i++) {
for (int j = 0; j <= endPosKolom; j++) {
GameObject gameobj = GameObject.Find("blocks[" + i + "," + j + "]");
if (mPoolGroup.Length < 1)
{
System.Array.Resize<PoolGroup>(ref mPoolGroup, mPoolGroup.Length + 1);
mPoolGroup[mPoolGroup.Length - 1] = new PoolGroup(gameobj.name);
}
else {
int k = 0;
bool ketemu = false;
while (k <= mPoolGroup.Length - 1) {
if (mPoolGroup[k].IfExistMemberName(gameobj.name)) { ketemu = true; }
k++;
}
if (!ketemu) {
System.Array.Resize<PoolGroup>(ref mPoolGroup, mPoolGroup.Length + 1);
mPoolGroup[mPoolGroup.Length - 1] = new PoolGroup(gameobj.name);
}
}
}
}
bool adaMatch3 = false;
if (mPoolGroup.Length >= 1) {
for (int i = 0; i <= (mPoolGroup.Length - 1); i++) {
if (mPoolGroup[i].IfMatch3()) {
mPoolGroup[i].ProcessMemberMatch();
if (!adaMatch3) { adaMatch3 = true; }
}
}
}
if (adaMatch3) {GlobalVarScript.ReOrganize();}
}
Answer by golddevrush · Jan 03, 2015 at 05:58 PM
i found the answer myself. according to my test and my logic, it happen because the update of frame. when i change using class to a struct and using a script that control in-game status every update. it response as i want (though it lack of animation hehe).