Cannot make bricks on a grid move. something wrong with my methods
Hey guys!
Iīm trying to create a kind of a puzzle game, where I have several bricks on a grid (6x6). The bricks can only move if there is a empty space located next to it. In other words, there is just one empty space all through the game and you use that one to move the bricks around on the grid.
I have an issue with my methods though. Iīm pretty sure that the correct point isnīt being "sent" to the other methods from my getPieceAtPoint method. In that method I specify which brick on the grid is being touched, but as I said... I think I declared the rest wrong.
I think as well that I did something wrong with the if-statements in my validemode method, but Iīm not sure what except that I suspect I shouldnīt have used center but something else entirely.
The code is successfully built and running without errors or warning but the movement doesnīt seem to work
//Get the point on the screen where the touch was made
-(void) getPieceAtPoint:(CGPoint) point{
CGRect touchRect = CGRectMake(point.x, point.y, 1.0, 1.0);
NSLog(@"getPieceAtPoint");
for (int y = 0; y < BRICKHEIGHT; y++){
for (int x = 0; x < BRICKWIDTH; x++){
if( CGRectIntersectsRect([grid[x][y] frame], touchRect) ){
[self movePiece:grid[x][y] withAnimation:YES];
NSLog(@"X: %d" , x);
NSLog(@"Y: %d" , y);
}
}
}
}
Code:
-(ShuffleMove) validMove:(UIImageView *) brickgrid{
NSLog(@"ValidMove");
// blank spot above current piece
if( brickgrid.center.x == blankPosition.x && brickgrid.center.y == blankPosition.y+1 ){
return UP;
NSLog(@"UP");
}
// bank splot below current piece
if( brickgrid.center.x == blankPosition.x && brickgrid.center.y == blankPosition.y-1 ){
return DOWN;
NSLog(@"Down");
}
// bank spot left of the current piece
if( brickgrid.center.x == blankPosition.x+1 && brickgrid.center.y == blankPosition.y ){
return LEFT;
NSLog(@"Left");
}
// bank spot right of the current piece
if( brickgrid.center.x == blankPosition.x-1 && brickgrid.center.y == blankPosition.y ){
return RIGHT;
NSLog(@"Right");
}
return NONE;
}
Code:
-(void) movePiece: (UIImageView *)brickgrid inDirectionX: (int) dx inDirectionY: (int) dy withAnimation: (BOOL) animate{
NSLog(@"MovePiece to Direction");
brickgrid.center = CGPointMake( brickgrid.center.x+dx,brickgrid.center.y+dy);
blankPosition = CGPointMake( blankPosition.x-dx, blankPosition.y-dy );
int x = brickgrid.center.x;
int y = brickgrid.center.y;
if( animate ){
[UIView beginAnimations:@"frame" context:nil];
}
brickgrid.frame = CGRectMake((BRICKWIDTH)*x, (BRICKHEIGHT)*y,
BRICKWIDTH, BRICKHEIGHT );
if( animate ){
[UIView commitAnimations];
}
}
i would use NSMutableArray, and not C-style array.
Ok... and will that also solve the issue with the if statements in the validmode method or just the issue that I have with not being able to get the other methods to get the correct point of the bricks?
sorry, but i don't understand what the method should do, and what you are trying to do.
The method is used for directions... in other words which direction the bricks should move... but only if they are next to a empty space. In other words the method checks if the "accessed" brick is next to a empty space
sorry, but i don't understand what the method should do, and what you are trying to do.
I modified my code a little by putting in a new CGPoint called currentPosition, which is (should be) the current position of the brick that was touched but I still canīt get it to work...
...maybe Iīm doing it all wrong.
the error message that I now get is error: subscripted value is neither array nor pointer
Itīs obvious why I get that specificmessage on the currentPosition = brickgrid[x][y]; in the FOR-loops but I have no idea how to be able to get the grid[x][y] from the getAtPoint method to the other methods...
...to be able to move that specific brick that was touched
You said earlier that it would be better with a NSMutableArray but how do you create a grid with an NSMutableArray?
Sorry if I sound clueless.
The thing is that I cannot find another way to make a grid than the way I made it
(or make the pieces inside the grid move when touched by the player)
Maybe there are other (better) ways to make all of this?
CGPoint currentPosition;
Code:
-(int) validMove:(UIImageView *) brickgrid{
NSLog(@"ValidMove");
for (int y = 0; y < BRICKHEIGHT; y++){
for (int x = 0; x < BRICKWIDTH; x++){
currentPosition = brickgrid[x][y];
}
}
// blank spot above current brick
if( currentPosition.x == blankPosition.x && currentPosition.y == blankPosition.y+1 ){
return UP;
NSLog(@"UP");
}
// bank splot below current brick
if( currentPosition.x == blankPosition.x && currentPosition.y == blankPosition.y-1 ){
return DOWN;
NSLog(@"Down");
}
// bank spot left of the current brick
if( currentPosition.x == blankPosition.x+1 && currentPosition.y == blankPosition.y ){
return LEFT;
NSLog(@"Left");
}
// bank spot right of the current brick
if( currentPosition.x == blankPosition.x-1 && currentPosition.y == blankPosition.y ){
return RIGHT;
NSLog(@"Right");
}
return NONE;
}
Code:
-(void) movePiece: (UIImageView *)brickgrid inDirectionX: (int) dx inDirectionY: (int) dy withAnimation: (BOOL) animate{
NSLog(@"MovePiece to Direction");
for (int y = 0; y < BRICKHEIGHT; y++){
for (int x = 0; x < BRICKWIDTH; x++){
currentPosition = brickgrid[x][y];
}
}
currentPosition = CGPointMake( currentPosition.x+dx,currentPosition.y+dy);
blankPosition = CGPointMake( blankPosition.x-dx, blankPosition.y-dy );
int x = currentPosition.x;
int y = currentPosition.y;
if( animate ){
[UIView beginAnimations:@"frame" context:nil];
}
brickgrid.frame = CGRectMake((BRICKWIDTH)*x, (BRICKHEIGHT)*y,
BRICKWIDTH, BRICKHEIGHT );
if( animate ){
[UIView commitAnimations];
}
}
sorry, but i don't understand what the method should do, and what you are trying to do.
Iīve tried out my solution and it worked. All of the methods got the X- & Y-coordinates from the getPieceAtPoint method.
The solution I tried was to send the X- & Y-coordinates instead of the UIImageView to the rest of my methods. I dodnīt know why I didnīt try this in first place.
In other words... I modified the "method entry" so they are now getting (int) brickX YPosition: (int) brickY instead of getting the (UIImageView *)brickgrid.
The last issue that I now have is to make the bricks animate upon touch. To switch places with the blank area on the grid.
I figured out that the problem lies in the method below and in the row which is slashed (//).
My way of thinking with the // part got the bricks to move to the upper left corner of the screen and minimize along the way (instead of moving to the empty location on the grid and staying in the same size)
the bricks move now upon touch (swap places with the blank area) but a new issue appeared... with the first touch. I will create a new thread for that part.
I'm sorry to haven't helped you, but really i still miss something to understand the problem that you had.
However good work, finally someone that can solve a problem keeping trying himself.
No worries, you helped me more than you think, both with this thread and the previous one.
the problem was that I didnīt know how to send the X-Y coordinates from the getpieceatpoint method to the rest of my methods so they could perform their "job" (in other words move the brick that I touched and swap itīs current location on the grid with the location of the blank space).
I think my previous explanations sucked... sorry for that.. and hopefully this one is better and could give you an perspective of what the problem was