Hey guys!
I need your help with solving this issue.
I tried the COCOS2D forum a few weeks back but got no answer. I really hope that someone at this forum will be able to help me.
I have a grid (6x7). I have 41 bricks on it plus 1 blank position. I'm trying to make a "3 or more in a row" type of game.
The bricks can only move to the blank position.
The issue that I have is that the blank position is being included as a "matching brick" when the method below looks for matches. How do I make the blank position stay neutral instead of being included into the matching part?
I've tried with adding this if statement into the part where "none matched" bricks are being added into a new group but it fails to work. it either makes the game to crash, starts to match wrong bricks or nor matching happens at all.
Code:
if(blankPosition.x == currPosition.x && blankPosition.y ==
currPosition.y){
continue; //Tried with break and return as well.
}
This is the looking for matches method:
Code:
-(void)checkGroups:(bool)firstTime{
int curHGroup = 0;
int curVGroup = 0;
int lastGroup =-1;
NSMutableArray * groupings = [[NSMutableArray alloc]init];
for(int i =0; i< GRID_WIDTH ; i++)
{
for(int j =0; j< GRID_HEIGHT ; j++)
{
Bricks * d = (Bricks *)grid[i][j];
d.disappearing = NO;
Bricks * l =nil;
Bricks * t =nil;
CGPoint currPosition = CGPointMake(i,j);
if(i>0)
l = (Bricks *)grid[i-1][j];
if(j>0)
t = (Bricks *)grid[i][j-1];
//IF there is a previous brick in the grid we compare the actual
one with that one. If they
//are of the same type we add it to that group.
//If not, we create a new horizontal group and add the brick to
that one.
if(l && l.bricksType == d.bricksType)
{
[[groupings objectAtIndex:l.curHGroup] addObject:grid[i][j]];
grid[i][j].curHGroup = l.curHGroup;
}
else {
if(blankPosition.x == currPosition.x && blankPosition.y ==
currPosition.y){
continue;
}
else{
curHGroup = lastGroup +1;
NSMutableSet * group = [[NSMutableSet alloc]init];
[groupings addObject:group];
[group release];
[[groupings objectAtIndex:curHGroup] addObject:grid[i][j]];
grid[i][j].curHGroup = curHGroup;
lastGroup = curHGroup;
}
}
//The same for grouping vertically
if(t && t.bricksType == d.bricksType)
{
[[groupings objectAtIndex:t.curVGroup] addObject:grid[i][j]];
grid[i][j].curVGroup = t.curVGroup;
}
else{
if(blankPosition.x == currPosition.x && blankPosition.y ==
currPosition.y){
continue;
}
else{
curVGroup = lastGroup+1;
NSMutableSet * group2 = [[NSMutableSet alloc]init];
[groupings addObject:group2];
[group2 release];
[[groupings objectAtIndex:curVGroup] addObject:grid[i][j]];
grid[i][j].curVGroup = curVGroup;
lastGroup = curVGroup;
}
}
}
}
BOOL moveBricks = NO;
for (NSMutableSet * n in groupings)
{
if([n count]>=3)
{
for(Bricks * c in n)
{
c.disappearing = YES;
moveBricks = YES;
[c.mySprite setOpacity:0];
[c.mySprite runAction:[CCFadeOut actionWithDuration:0.5]];
}
}
}
//We are done with the groupings array, release it.
[groupings release];
if(moveBricks)
{
[self schedule:@selector(moveBricksDown) interval:0.5];
}
else
{
self.allowTouch = YES;
CCLOG(@"self.allowTouch = YES");
}
Thanks in advance!
***Update********
It seems that I was wrong. :S
The issue (as it seems after I had a revelation yesterday) isn't that the blank position is ACTING as matching brick BUT actually IS a brick but invisible and that's why it is causing this issue.
In other words....
...I wasn't able to create a empty space that was neutral. I created a empty space by removing the visibility of a specific brick without removing the attributes of the brick, which is causing it to still act as a brick.
I will create a new thread about it since this thread is totally wrong...